MEESHO Coding Question – Solved

11 Live
Vowel SubString Given a string composed of lowercase letters within the ASCII range 'a'-'z', determine the number of substrings that consist solely of vowels, where each vowel appears at least once. The vowels are ['a', 'e', 'i', 'o', 'u']. A substring is defined as a contiguous sequence of characters within the string. Example s = 'aeioaexaaeuiou' There is a substring to the left that is made of vowels, 'aeioae', which is followed by an 'x'. Since 'x' is not a vowel, it cannot be included in the substring, and this substring does not contain all of the vowels. It is not a qualifying substring. Moving to the right, there are four substrings that do qualify: 'aaeuiou', 'aaeuid', 'aeuiou', and 'aeuio'. Function Description Complete the function vowelSubstring in the editor with the following parameter(s): string s: a string Returns int: the number of substrings that consist of vowels only ('a', 'e', 'i', 'o', 'u') where every vowel appears at least once. Constraints 1 ≀ size_of(s) ≀ 10^5 s[i] is in the range ascii['a'-'z'] (where 0 ≀ i < size_of(s))

Asked in: MEESHO

Image of the Question

Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


def vowelsubstring(s):
    # Write your code here
    def solve(s):
        vowels = "aeiou"
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To solve the problem of counting the number of substrings in a given string that consist solely of vowels and contain all five vowels ('a', 'e', 'i', 'o', 'u') at least once, you need to carefully approach the problem in a way that handles potentially large inputs efficiently.

Step 1: Understand the problem clearly
You want to find substrings that:
- Are composed only of vowels from the set {'a', 'e', 'i', 'o', 'u'}.
- Contain every vowel in this set at least once.
- Substrings are contiguous segments of the original string.

Non-vowel characters act as natural delimiters that break the string into segments where only vowels appear. Each such segment can be independently analyzed because any substring containing a non-vowel would not be valid.

Step 2: Decompose the problem into manageable parts
Since the input string can be large (up to 10^5 characters), an efficient solution is required, ideally O(n) or O(n log n).

A good initial step is to:
- Split the string into vowel-only segments by breaking the string at every non-vowel character.
- Each segment now contains only vowels.

Your task then reduces to:
- For each vowel-only segment, count the substrings that contain all vowels at least once.

Step 3: Focus on counting qualifying substrings in each vowel-only segment
Given a vowel-only substring, you want to find all substrings containing all vowels at least once. This is a classic substring problem often solved with a "sliding window" or "two-pointer" approach.

Step 4: Using the sliding window approach
The sliding window technique maintains two pointers that represent the current substring window. You expand the right pointer to include characters until all vowels are included at least once, then move the left pointer to minimize the window while still keeping all vowels.

Details:
- Maintain a frequency count of vowels in the current window.
- Expand the window by moving the right pointer until all vowels appear at least once.
- When you have a valid window containing all vowels, you can infer how many substrings ending at the right pointer satisfy the condition.
- Move the left pointer forward to find other possible substrings.

Step 5: Counting substrings using the sliding window
When you find a valid window [left, right], all substrings starting at any index between left and right (inclusive) and ending at right will contain all vowels.

This means you can add (left_substring_count) substrings at this point. Careful calculation is needed to count how many substrings can be formed given the current window.

Step 6: Repeat for each vowel-only segment
Since segments are separated by non-vowels, you can run the sliding window process independently on each segment and sum up the counts.

Step 7: Handle edge cases
- If the input string contains no vowel-only segments that have all vowels, the output is zero.
- Single-character vowel-only segments obviously cannot contain all vowels, so they contribute zero.
- Make sure to handle segments that are just long enough to contain all vowels.

Step 8: Optimize for large inputs
The sliding window approach will ensure you process each character a constant number of times, leading to O(n) complexity overall.
Use a data structure like a dictionary or an array of size 5 to keep vowel counts.

Step 9: Validate with example
For s = "aeioaexaaeuiou":
- Split into vowel-only substrings at non-vowels like 'x'.
- Analyze each segment:
- "aeioae" lacks all vowels, so it won't contribute.
- "aaeuiou" has all vowels and you use the sliding window approach to count qualifying substrings.

Step 10: Summary
- Break input into vowel-only segments by splitting at non-vowels.
- For each segment, use sliding window to find substrings containing all vowels at least once.
- Maintain counts of vowels in the current window and update counts as you move pointers.
- Add counts of qualifying substrings to the answer.
- Return the sum over all segments.

This approach leverages the natural segmentation by non-vowels and the efficient sliding window technique to solve the problem within the constraints.
```


Related Questions