forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestsubstringnorepeatingchar.java
More file actions
executable file
·39 lines (38 loc) · 1.01 KB
/
longestsubstringnorepeatingchar.java
File metadata and controls
executable file
·39 lines (38 loc) · 1.01 KB
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
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s.length()==0){
return 0;
}
int max =1;
int map[] = new int[26];
for(int i=0;i<26;i++){
map[i]=-1;
}
int start = 0;
int end = 1;
map[s.charAt(start)-'a']=start;
while(true){
if(end==s.length()){
break;
}
if(map[s.charAt(end)-'a']==-1){
map[s.charAt(end)-'a']=end;
end++;
if(end-start>max){
max = end-start;
}
}else{
int t = map[s.charAt(end)-'a'];
for(int i=start;i<=t;i++){
map[s.charAt(i)-'a']=-1;
}
start = t+1;
map[s.charAt(end)-'a']=end;
end++;
}
}
return max;
}
}