-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
58 lines (55 loc) · 1.47 KB
/
main.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
56
57
58
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
unordered_set<char> seen;
for (int l = 0, r = 0; r < size(s); ++r) {
while (seen.count(s[r])) {
seen.erase(s[l++]);
}
seen.insert(s[r]);
ans = max(ans, int(size(seen)));
}
return ans;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int result = 0;
unordered_map<char, bool> seen;
for (int l = 0, r = 0; r < s.size(); ++r) {
while (seen[s[r]]) seen[s[l++]] = false;
seen[s[r]] = true;
result = max(result, r - l + 1);
}
return result;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.size();
unordered_map<char, int> charCount;
int result = 0;
for (int l = 0, r = 0; r < n; ++r) {
++charCount[s[r]];
while (charCount[s[r]] > 1) --charCount[s[l++]];
result = max(result, r - l + 1);
}
return result;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int result = 0;
unordered_map<char, int> lastPosPlusOne;
for (int l = 0, r = 0; r < s.size(); ++r) {
l = max(l, lastPosPlusOne[s[r]]);
result = max(result, r - l + 1);
lastPosPlusOne[s[r]] = r + 1;
}
return result;
}
};