-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestSubStr.java
34 lines (29 loc) · 1.07 KB
/
LongestSubStr.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
import java.util.ArrayList;
import java.util.List;
class LongestSubStr {
public static void main(String[] args) {
LongestSubStr s = new LongestSubStr();
System.out.println(s.lengthOfLongestSubstring("abcabcbb"));
System.out.println(s.lengthOfLongestSubstring("bbbbb"));
System.out.println(s.lengthOfLongestSubstring("pwwkew"));
System.out.println(s.lengthOfLongestSubstring(""));
System.out.println(s.lengthOfLongestSubstring("pwwwkewpwdweweadfasfsaf"));
}
public int lengthOfLongestSubstring(String s) {
int max=0, j = 0;
for (int i = 0; i < s.length(); i++) {
List<Character> list = new ArrayList<>();
char start = s.charAt(i);
list.add(start);
for (j = i+1; j < s.length(); j++) {
if (list.contains(s.charAt(j))) {
break;
}
list.add(s.charAt(j));
}
int curSize = j - i;
max = max > curSize ? max : curSize;
}
return max;
}
}