-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
28 lines (27 loc) · 714 Bytes
/
3.cpp
File metadata and controls
28 lines (27 loc) · 714 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
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> hash(130);
int l = 0,r = 1,res = 1;
if(s.size()<2)return s.size();
else{
hash[s[l]]++;
while(l < r && r < s.size()){
hash[s[r]]++;
if(hash[s[r]] > 1){
res = max(res,r-l);
while(hash[s[r]] > 1){
hash[s[l]]--;
l++;
}
}
if(r == s.size()-1){
res = max(res,r-l+1);
break;
}
r++;
}
}
return res;
}
};