-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubString.java
More file actions
60 lines (42 loc) · 1.75 KB
/
LongestSubString.java
File metadata and controls
60 lines (42 loc) · 1.75 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package node;
import java.util.HashSet;
public class LongestSubString {
public static void main(String[] args) {
//String s = "abcabcbb";
String s = "aab";
// String s = "pwwkew";
System.out.println(lengthOfLongestSubstring2(s));
}
public static int lengthOfLongestSubstring(String s) {
int res = 0;
HashSet<Character> set = new HashSet<>();
int start =0, end =0; //sliding window , two pointer, one is start, one is end
while ( start<s.length() && end < s.length()){
if (set.contains(s.charAt(end))){ //when there is a duplicate character found ,
set.remove(s.charAt(start)); // remove the first duplicate character
start++; // move pointer to next position
}else {
set.add(s.charAt(end)); // when there isn't any character found in set , add into set
res = Math.max(res, set.size()); // every time , add into new set, and check what is max size of set
end++; // move pointer to next
}
}
return res;
}
public static int lengthOfLongestSubstring2(String s) {
int max =0 ;
int start =0, end;
HashSet<Character> set = new HashSet<>();
for (end = 0; end < s.length() ; ) {
if (set.contains(s.charAt(end))){ //check found duplicated character in set , then remove this character ,and increase start pointer
set.remove(s.charAt(start));
start++;
}else {
set.add(s.charAt(end));
max = Math.max(max, set.size());
end++;
}
}
return max;
}
}