Skip to content

Commit cabd7a8

Browse files
authored
Create find-the-longest-substring-containing-vowels-in-even-counts.cpp
1 parent 67a39d1 commit cabd7a8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int findTheLongestSubstring(string s) {
7+
static const string VOWELS = "aeiou";
8+
int result = 0, mask = 0;
9+
vector<int> lookup(32, -2);
10+
lookup[0] = -1;
11+
for (int i = 0; i < s.length(); ++i) {
12+
const auto& pos = VOWELS.find(s[i]);
13+
mask ^= pos != string::npos ? 1 << pos : 0;
14+
if (lookup[mask] == -2) {
15+
lookup[mask] = i;
16+
}
17+
result = max(result, i - lookup[mask]);
18+
}
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)