-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay12.java
More file actions
33 lines (30 loc) · 920 Bytes
/
Day12.java
File metadata and controls
33 lines (30 loc) · 920 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
31
32
33
package month3;
import java.util.HashMap;
public class Day12 {
public static void main(String[] args) {
Day12 tool = new Day12();
String s = "pwwkew";
System.out.println(tool.lengthOfLongestSubstring(s));
}
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0){
return 0;
}
// 0 -> a 1 -> b
HashMap<Character,Integer> map = new HashMap<>();
int max = 1;
int preLen = 1;
map.put(s.charAt(0),0);
for (int i = 1; i < s.length() ; i++) {
char ch = s.charAt(i);
if (map.containsKey(ch)){
preLen = Math.min(preLen+1,i - map.get(ch));
}else {
preLen = preLen+1;
}
max = Math.max(max,preLen);
map.put(ch,i);
}
return max;
}
}