#2062
Count Vowel Substrings of a String
EasyApproaches
💡
Intuition
Time O(n²)Space O(1)
The brute force approach involves generating all possible substrings of the input string and checking if they contain all five vowels. This method is straightforward but can be inefficient due to the large number of substrings.
⚙️
Algorithm
3 steps- 1Step 1: Iterate through each character in the string as a starting point for substrings.
- 2Step 2: For each starting point, generate all possible substrings until a consonant is encountered.
- 3Step 3: Check if the substring contains all five vowels. If it does, increment the count.
solution.py12 lines
1def countVowelSubstrings(word):
2 vowels = set('aeiou')
3 count = 0
4 n = len(word)
5 for i in range(n):
6 if word[i] in vowels:
7 for j in range(i, n):
8 if word[j] not in vowels:
9 break
10 if vowels.issubset(set(word[i:j+1])):
11 count += 1
12 return countℹ
Complexity note: The time complexity is O(n²) because for each character, we may generate up to n substrings, leading to a quadratic number of checks. The space complexity is O(1) since we are using a fixed amount of extra space.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.