To solve this problem, we need to find the length of the longest substring in a given string where the number of vowels (a, e, i, o, u) is exactly k.
Approach
The key insight here is to use the prefix sum technique combined with a hash map to track the first occurrence of each prefix sum. This allows us to efficiently find the longest substring with exactly k vowels. Here's the detailed approach:
- Prefix Sum Calculation: Compute the prefix sum of vowels as we iterate through the string. The prefix sum at index i (denoted as
current_sum) represents the number of vowels from the start of the string up to the current character. - Hash Map for First Occurrence: Use a hash map to store the first occurrence of each prefix sum. This helps in finding the earliest index where the prefix sum was
current_sum - k, which gives the longest possible substring ending at the current index with exactly k vowels. - Check for Target Sum: For each current prefix sum, check if
current_sum - kexists in the hash map. If it does, the length of the substring between the current index and the index stored in the hash map is a candidate for the maximum length.
Solution Code
def longestSubstringWithKvowels(s: str, k: int) -> int:
vowels = {'a', 'e', 'i', 'o', 'u'}
current_sum = 0
first_occurrence = {0: 0} # Maps prefix sum to its first index
max_len = 0
for i in range(1, len(s) + 1):
# Update current_sum if the current character is a vowel
if s[i-1] in vowels:
current_sum += 1
# Check if we can find a prefix sum that gives exactly k vowels
target = current_sum - k
if target in first_occurrence:
max_len = max(max_len, i - first_occurrence[target])
# Store the first occurrence of the current_sum if not already present
if current_sum not in first_occurrence:
first_occurrence[current_sum] = i
return max_len
Explanation
- Initialization: We start with
current_sumset to 0 (no vowels initially) and a hash mapfirst_occurrenceinitialized with {0:0} (since the prefix sum before the first character is 0 at index 0). - Iterate Through String: For each character in the string, we update
current_sumif the character is a vowel. - Target Check: For each
current_sum, we compute the target sum (current_sum - k). If this target exists in the hash map, the length of the substring from the index stored in the hash map to the current index is calculated and compared with the maximum length found so far. - Store First Occurrence: If the current prefix sum is not in the hash map, we store it with the current index to ensure we always have the earliest occurrence, which helps in maximizing the substring length.
This approach runs in O(n) time complexity, where n is the length of the string, and O(n) space complexity in the worst case (for the hash map). This makes it efficient and suitable for large input sizes.
Example Walkthrough
Let's take the string "leetcode" and k=3:
- The prefix sum sequence is: [0,0,1,2,2,2,3,3,4]
- For
current_sum=3(at index6), the target is3-3=0(found at index0). The length is6-0=6. - For
current_sum=3(at index7), the target is 0, so length is7-0=7(the longest substring here).
This confirms that the solution correctly finds the longest substring with exactly k vowels.
The code handles all edge cases, including k=0 (longest substring with no vowels) and cases where k is larger than the total number of vowels (returns 0). It's a robust and efficient solution.

作者声明:本文包含人工智能生成内容。