-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path159-longest-substring-with-two-distinct-chars.cpp
55 lines (54 loc) · 1.71 KB
/
159-longest-substring-with-two-distinct-chars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Faster Solution as we don't clear the hashmap each time
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
unordered_map<char, int> uniqueChars;
int longest = 0;
int N = s.size();
if (N <= 2) return N;
int left = 0;
int right = 0;
while (right < N) {
if (uniqueChars.size() <= 2) {
uniqueChars[s[right]] = right;
}
if (uniqueChars.size() == 3) {
auto it = min_element(uniqueChars.begin(), uniqueChars.end(),
[](const auto& l, const auto& r) {
return l.second < r.second;
});
int leftmost = it->second;
uniqueChars.erase(s[leftmost]);
left = leftmost + 1;
}
longest = max(longest, right - left + 1);
right++;
}
return longest;
}
};
// Slower solution becasue set gets cleared each time
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
unordered_set<char> uniqueChars;
int longest = 0;
int N = s.size();
if (N <= 1) return N;
for (int j = 0; j < N; j++) {
int currSize = 0;
for (int i = j; i < N; i++) {
char c = s[i];
uniqueChars.insert(c);
if (uniqueChars.size() <= 2) {
currSize++;
} else {
uniqueChars.clear();
break;
}
}
longest = longest < currSize ? currSize : longest;
}
return longest;
}
};