-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
36 lines (32 loc) · 734 Bytes
/
Copy path3.cpp
File metadata and controls
36 lines (32 loc) · 734 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
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
class Solution {
public:
set<char> index;
int len = 0;
int lengthOfLongestSubstring(string s) {
int i = 0, j = 0;
while (i < s.size() && j < s.size()) {
if (i < j && index.count(s[j]) == 0) {
index.insert(s[j]);
j += 1;
} else if (i < j && index.count(s[j])) {
len = max(len, j - i);
while (s[i] != s[j]) index.erase(s[i++]);
index.erase(s[i++]);
} else if (i == j) {
index.insert(s[j]);
j += 1;
}
}
return max(len, j - i);
}
};
int main() {
Solution s;
cout << s.lengthOfLongestSubstring("abcabcbb") << endl;
}