AMAZON Coding Question – Solved

7 Live
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, 'aeioee', which is followed by an 'x'. Since 'x' is not a vowel, it cannot be included in the substring. This substring does not contain all of the vowels, so it is not a qualifying substring. Moving to the right, there are four substrings that do qualify: 'aaeuiou', 'aaeuio', 'aeuiou', and 'aeuio'. Function Description Complete the function vowelSubstring in the editor with the following parameter: - 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⁵ - s[i] is in the range ASCII 'a'-'z' (where 0 ≤ i < size_of(s)) Input Format For Custom Testing Sample Case 0 Sample Input For Custom Testing STDIN aaeiouxa Sample Output 2 Explanation s = "aaeiouxa" There are two qualifying substrings: s[0:6] = "aaeiou" s[1:6] = "aeiou"

Asked in: AMAZON

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def vowelsubstring(s):
    answer = 0
    n = len(s)
    def solve(s):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach the problem of counting substrings consisting solely of vowels where every vowel ('a', 'e', 'i', 'o', 'u') appears at least once, you need to carefully consider both the structure of the input string and how to efficiently identify valid substrings that meet the criteria.

---

Step 1: Understand the Problem Constraints and Definitions

- The string can contain both vowels and consonants.
- We are only interested in substrings that contain vowels exclusively; any consonant breaks the substring.
- Among these vowel-only substrings, we want to count those that contain all five vowels at least once.
- The length of the string can be up to 10^5, so any naive approach that checks all substrings explicitly would be too slow.
- The vowels are fixed: 'a', 'e', 'i', 'o', 'u'.

---

Step 2: Segment the String by Non-Vowel Characters

- Since consonants break the substring, a natural step is to split the string into vowel-only segments separated by consonants.
- For example, given the string, scan it from left to right, and whenever you encounter a consonant, end the current vowel segment and start fresh after it.
- Each vowel segment is thus a continuous substring made entirely of vowels.
- This reduces the problem: now, instead of checking the whole string, focus only on these vowel-only segments.
- Each segment can be handled independently to find qualifying substrings.

---

Step 3: Count Qualifying Substrings Within Each Vowel-Only Segment

- For each vowel-only segment, find all substrings that contain all vowels at least once.
- Since the vowels are fixed and limited to five, you can think of this as a sliding window problem:
- Use two pointers (start and end) that define the current window within the segment.
- Move the end pointer forward, adding characters to the current window and keeping track of vowel counts.
- Use a frequency map or array indexed by vowels to count how many times each vowel appears in the current window.
- Once the window contains all five vowels at least once, you know that any substring starting at `start` and ending at or beyond `end` will also contain all vowels.

---

Step 4: Sliding Window Expansion and Contraction

- Expand the window by moving the `end` pointer to the right, updating vowel counts.
- When the window contains all vowels, try to contract it from the left to find the smallest window still containing all vowels.
- Each time the window satisfies the condition (all vowels present), all substrings starting at `start` and ending at any position from `end` to the end of the segment are valid.
- You can count the number of valid substrings contributed by this window by calculating how many end positions are possible for this start.
- Then move the `start` pointer right to try finding another smaller window still containing all vowels.
- Continue this process until the entire segment is processed.

---

Step 5: Efficiently Checking the Condition of All Vowels Present

- Keep a count of how many distinct vowels have nonzero counts in the current window.
- When adding a vowel that increases its count from zero to one, increment a distinct vowels counter.
- When removing a vowel that decreases its count from one to zero, decrement that counter.
- When the distinct vowels counter reaches 5, it means the window contains all vowels.

---

Step 6: Combining Results from All Segments

- Process each vowel-only segment independently with the sliding window method to count valid substrings.
- Sum up the counts from all segments to get the final answer.

---

Step 7: Edge Cases and Performance Considerations

- Handle the case where the string contains no vowel-only substrings with all vowels.
- Handle very short segments where it is impossible to contain all vowels.
- Ensure the sliding window operations and frequency updates are done in O(1) time to keep overall complexity manageable.
- Since you only iterate over each character a limited number of times (once moving `end`, once moving `start`), the time complexity is approximately O(N).

---

Step 8: Summary of the Thought Process

- Break the problem into manageable pieces by splitting the input string at consonants.
- Use a two-pointer sliding window on vowel-only segments to find minimal windows containing all vowels.
- Count how many substrings satisfy the condition based on window size and positions.
- Aggregate counts over all vowel-only segments.
- Optimize using frequency counts and a distinct vowels counter for fast checks.

This approach leverages the fixed vowel set, the natural segmentation by consonants, and the classic sliding window technique to efficiently find and count qualifying substrings without enumerating all substrings explicitly, making it suitable for large inputs.
```


Related Questions