-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path159.cpp
More file actions
24 lines (24 loc) · 733 Bytes
/
159.cpp
File metadata and controls
24 lines (24 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int char2idx(const char& c) {
if (c >= 'a' && c <= 'z') return c - 'a';
else return c - 'A' + 26;
}
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> counting(52, 0);
int count = 0;
int res = 1;
int left = 0;
for (int right = 0; right < s.size(); ++right) {
if (counting[char2idx(s[right])] == 0) count++;
counting[char2idx(s[right])]++;
while (count > 2) {
if (counting[char2idx(s[left])] == 1) count--;
counting[char2idx(s[left])]--;
left++;
}
res = max(res, right - left + 1);
}
return res;
}
};