-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet3.cpp
More file actions
30 lines (30 loc) · 881 Bytes
/
Copy pathleet3.cpp
File metadata and controls
30 lines (30 loc) · 881 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
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int index[300] ;
int str_len = s.length() ;
int res = 0 ;
for(int i=0 ;i<300 ;i++)
index[i] = -1 ;
for(int i=0 ;i<str_len ;i++){
for(int j=i ;j<str_len ;j++){
if(index[s[j]] == -1 || index[s[j]]<i || index[s[j]] == j){
index[s[j]] = j ;
if(j==str_len-1){
if(j-i+1>res){
res = j-i+1 ;
}
return res ;
}
}else{
int ti = index[s[j]] ;
if(j-i>res)
res = j-i ;
i = ti ;
break ;
}
}
}
return res ;
}
};