Skip to content
Advertisement

Finding Subarrays of Vowels from a given String

You are given a string S, and you have to find all the amazing substrings of S.

Amazing Substring is one that starts with a vowel (a, e, i, o, u, A, E, I, O, U).

Input

The only argument given is string S.

Output

Return a single integer X mod 10003, here X is number of Amazing Substrings in given string.

Constraints

  • 1 <= length(S) <= 1e6
  • S can have special characters

Example

JavaScript

I have implemented the following algo for the above Problem.

JavaScript

Above Solution works fine for strings of normal length but not for greater length.

For example,

JavaScript

Above Algo outputs 1630 subarrays but the expected answer is 1244.

Please help me improving the above algo. Thanks for the help

Advertisement

Answer

Focus on the required output: you do not need to find all of those substrings. All you need is the quantity of substrings.

Look again at your short example, ABEC. There are two vowels, A and E.

  • A is at location 0. There are 4 total substrings, ending there and at each following location.
  • E is at location 2. There are 2 total substrings, ending there and at each following location.

2+4 => 6

All you need do is to find the position of each vowel, subtract from the string length, and accumulate those differences:

JavaScript

Output:

JavaScript

In a single command:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement