forked from ghostpye07/Python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.CPP
More file actions
26 lines (26 loc) · 685 Bytes
/
Copy pathproblem.CPP
File metadata and controls
26 lines (26 loc) · 685 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
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
int n = s.size();
int temp = 0;
int h[128] = {0};
for(int i = 0;i<n;i++){
if(h[int(s[i])] > 0)
{
if(h[int(s[i])] >= 2)
for(int j = h[int(s[i])] - 2 ;j >= (i - temp);j--){
h[int(s[j])] = 0;
}
ans = max(ans,temp);
temp = i - h[int(s[i])] + 1;
h[int(s[i])] = i + 1;
}
else{
temp++;
h[int(s[i])] = i + 1;
}
}
return max(ans,temp);
}
};