Skip to content

Commit e1ed551

Browse files
committed
Sync LeetCode submission Runtime - 1787 ms (65.90%), Memory - 25.4 MB (9.25%)
1 parent b44a117 commit e1ed551

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>You are given a string <code>word</code> and a <strong>non-negative</strong> integer <code>k</code>.</p>
2+
3+
<p>Return the total number of <span data-keyword="substring-nonempty">substrings</span> of <code>word</code> that contain every vowel (<code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>) <strong>at least</strong> once and <strong>exactly</strong> <code>k</code> consonants.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aeioqq&quot;, k = 1</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>There is no substring with every vowel.</p>
16+
</div>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aeiou&quot;, k = 0</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
24+
25+
<p><strong>Explanation:</strong></p>
26+
27+
<p>The only substring with every vowel and zero consonants is <code>word[0..4]</code>, which is <code>&quot;aeiou&quot;</code>.</p>
28+
</div>
29+
30+
<p><strong class="example">Example 3:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">word = &quot;</span>ieaouqqieaouqq<span class="example-io">&quot;, k = 1</span></p>
34+
35+
<p><strong>Output:</strong> 3</p>
36+
37+
<p><strong>Explanation:</strong></p>
38+
39+
<p>The substrings with every vowel and one consonant are:</p>
40+
41+
<ul>
42+
<li><code>word[0..5]</code>, which is <code>&quot;ieaouq&quot;</code>.</li>
43+
<li><code>word[6..11]</code>, which is <code>&quot;qieaou&quot;</code>.</li>
44+
<li><code>word[7..12]</code>, which is <code>&quot;ieaouq&quot;</code>.</li>
45+
</ul>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>5 &lt;= word.length &lt;= 2 * 10<sup>5</sup></code></li>
53+
<li><code>word</code> consists only of lowercase English letters.</li>
54+
<li><code>0 &lt;= k &lt;= word.length - 5</code></li>
55+
</ul>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Approach 1: Sliding Window
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def _is_vowel(self, c):
8+
return c in ['a', 'e', 'i', 'o', 'u']
9+
10+
def countOfSubstrings(self, word: str, k: int) -> int:
11+
num_valid_substrings = 0
12+
start = end = 0
13+
vowel_count = {}
14+
consonant_count = 0
15+
next_consonant = [0] * len(word) # Array to compute index of next consonant for all indices
16+
next_consonant_index = len(word)
17+
18+
for i in range(len(word) - 1, -1, -1):
19+
next_consonant[i] = next_consonant_index
20+
if not self._is_vowel(word[i]):
21+
next_consonant_index = i
22+
23+
while end < len(word):
24+
new_letter = word[end]
25+
if self._is_vowel(new_letter):
26+
vowel_count[new_letter] = vowel_count.get(new_letter, 0) + 1
27+
else:
28+
consonant_count += 1
29+
30+
while consonant_count > k:
31+
# Shrink window
32+
start_letter = word[start]
33+
if self._is_vowel(start_letter):
34+
vowel_count[start_letter] -= 1
35+
if vowel_count[start_letter] == 0:
36+
del vowel_count[start_letter]
37+
else:
38+
consonant_count -= 1
39+
start += 1
40+
41+
while start < len(word) and len(vowel_count) == 5 and consonant_count == k:
42+
# Try to shrink if window is valid
43+
num_valid_substrings += next_consonant[end] - end
44+
start_letter = word[start]
45+
if self._is_vowel(start_letter):
46+
vowel_count[start_letter] -= 1
47+
if vowel_count[start_letter] == 0:
48+
del vowel_count[start_letter]
49+
else:
50+
consonant_count -= 1
51+
52+
start += 1
53+
54+
end += 1
55+
56+
return num_valid_substrings
57+

0 commit comments

Comments
 (0)