-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestSubstringNoRepeat.java
51 lines (45 loc) · 1.61 KB
/
LongestSubstringNoRepeat.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
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public int lengthOfLongestSubstring(String s) {
char[] chars = s.toCharArray();
int[] map = new int[256];
Arrays.fill(map, -1);
int firstIdx = 0;
int max = 0;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (map[c] >= firstIdx) {
max = Math.max(max, i - firstIdx);
firstIdx = map[c] + 1;
}
map[c] = i;
}
max = Math.max(max, chars.length - firstIdx);
return max;
}
// public int lengthOfLongestSubstring(String s) {
// int max = 0;
// StringBuilder currStr = new StringBuilder();
// for (int i = 0; i < s.length(); i++) {
// char c = s.charAt(i);
// currStr = currStr.delete(0, currStr.toString().indexOf(c) + 1);
// currStr.append(c);
// max = Math.max(max, currStr.length());
// }
// return max;
// }
//Using Hashmap
// public int lengthOfLongestSubstring(String s) {
// int maxLength = 0;
// HashMap<Character, Integer> indices = new HashMap<Character, Integer>();
// int firstIdx = 0;
// for (int i = 0; i < s.length(); i++) {
// char c = s.charAt(i);
// if (indices.containsKey(c) && indices.get(c) >= firstIdx) {
// firstIdx = indices.get(c) + 1;
// }
// indices.put(c, i);
// maxLength = Math.max(maxLength, i - firstIdx + 1);
// }
// return maxLength;
// }
}