-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain003.java
40 lines (32 loc) · 961 Bytes
/
Main003.java
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
public class Main003 {
public static void main(String[] args){
System.out.println(new Main003().lengthOfLongestSubstring("audaudf"));
}
public int lengthOfLongestSubstring(String s) {
int[] chars = new int[256];
int start = 0;
if (s.length() <= 0){
return 0;
}
int maxLen = 1;
int i = 0;
for (i = 0; i < s.length(); i++){
int ch = (int) s.charAt(i);
if (chars[ch] != 0){
if (i - start > maxLen){
maxLen = i - start;
}
start = start+1;
i = start;
chars = new int[256];
chars[s.charAt(start)] = 1;
}else {
chars[ch] = 1;
}
}
if (i - start > maxLen){
maxLen = i - start;
}
return maxLen;
}
}