-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubStringWithoutRepeating.java
More file actions
55 lines (48 loc) · 1.7 KB
/
LongestSubStringWithoutRepeating.java
File metadata and controls
55 lines (48 loc) · 1.7 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
import java.util.HashMap;
/**
* LongestSubStringWithoutRepeating
*
* Given a string, find the length of the longest substring without repeating
* characters.
*/
public class LongestSubStringWithoutRepeating {
public static int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> map = new HashMap<>();
int ans = 0;
for (int i = 0, j = 0; j < s.length(); j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
public static String longestSubstring(String s) {
HashMap<Character, Integer> map = new HashMap<>();
int ans = 0;
int end = 0;
for (int i = 0, j = 0; j < s.length(); j++) {
if (map.containsKey(s.charAt(j))) {
if (map.get(s.charAt(j)) > i) {
i = map.get(s.charAt(j));
}
}
if ((j - i + 1) > ans) {
ans = j - i + 1;
end = j;
}
map.put(s.charAt(j), j + 1);
}
System.out.println("end = " + end);
return s.substring(end + 1 - ans, end + 1);
}
public static void main(String[] args) {
String s1 = "abcabcbb";
String s2 = "abcabcdb";
String s3 = "bbbbbb";
System.out.println(s1 + " " + lengthOfLongestSubstring(s1) + " " + longestSubstring(s1));
System.out.println(s2 + " " + lengthOfLongestSubstring(s2) + " " + longestSubstring(s2));
System.out.println(s3 + " " + lengthOfLongestSubstring(s3) + " " + longestSubstring(s3));
}
}