1371. Find the Longest Substring Containing Vowels in Even Counts LeetCode Solution
In this guide, you will get 1371. Find the Longest Substring Containing Vowels in Even Counts LeetCode Solution with the best time and space complexity. The solution to Find the Longest Substring Containing Vowels in Even Counts problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Longest Substring Containing Vowels in Even Counts solution in C++
- Find the Longest Substring Containing Vowels in Even Counts solution in Java
- Find the Longest Substring Containing Vowels in Even Counts solution in Python
- Additional Resources

Problem Statement of Find the Longest Substring Containing Vowels in Even Counts
Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ must appear an even number of times.
Example 1:
Input: s = “eleetminicoworoep”
Output: 13
Explanation: The longest substring is “leetminicowor” which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
Example 2:
Input: s = “leetcodeisgreat”
Output: 5
Explanation: The longest substring is “leetc” which contains two e’s.
Example 3:
Input: s = “bcbcbc”
Output: 6
Explanation: In this case, the given string “bcbcbc” is the longest because all vowels: a, e, i, o and u appear zero times.
Constraints:
1 <= s.length <= 5 x 10^5
s contains only lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1371. Find the Longest Substring Containing Vowels in Even Counts LeetCode Solution in C++
class Solution {
public:
int findTheLongestSubstring(string s) {
constexpr string_view kVowels = "aeiou";
int ans = 0;
int prefix = 0; // the binary prefix
unordered_map<int, int> prefixToIndex{{0, -1}};
for (int i = 0; i < s.length(); ++i) {
const int index = kVowels.find(s[i]);
if (index != -1)
prefix ^= 1 << index;
if (!prefixToIndex.contains(prefix))
prefixToIndex[prefix] = i;
ans = max(ans, i - prefixToIndex[prefix]);
}
return ans;
}
};
/* code provided by PROGIEZ */
1371. Find the Longest Substring Containing Vowels in Even Counts LeetCode Solution in Java
class Solution {
public int findTheLongestSubstring(String s) {
final String kVowels = "aeiou";
int ans = 0;
int prefix = 0; // the binary prefix
Map<Integer, Integer> prefixToIndex = new HashMap<>();
prefixToIndex.put(0, -1);
for (int i = 0; i < s.length(); ++i) {
final int index = kVowels.indexOf(s.charAt(i));
if (index != -1)
prefix ^= 1 << index;
prefixToIndex.putIfAbsent(prefix, i);
ans = Math.max(ans, i - prefixToIndex.get(prefix));
}
return ans;
}
}
// code provided by PROGIEZ
1371. Find the Longest Substring Containing Vowels in Even Counts LeetCode Solution in Python
class Solution:
def findTheLongestSubstring(self, s: str) -> int:
kVowels = 'aeiou'
ans = 0
prefix = 0 # the binary prefix
prefixToIndex = {0: -1}
for i, c in enumerate(s):
index = kVowels.find(c)
if index != -1:
prefix ^= 1 << index
prefixToIndex.setdefault(prefix, i)
ans = max(ans, i - prefixToIndex[prefix])
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.