-
Notifications
You must be signed in to change notification settings - Fork 43
/
LongestSubstringWithoutRepeatingCharacters.java
163 lines (132 loc) · 4.2 KB
/
LongestSubstringWithoutRepeatingCharacters.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package LeetCodeJava.HashTable;
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class LongestSubstringWithoutRepeatingCharacters {
// V0
// IDEA : HASHMAP + SLIDING WINDOW
public int lengthOfLongestSubstring(String s) {
/**
* key : element
* value : element count
*/
Map<String, Integer> map = new HashMap();
// left pointer
int left = 0;
// right pointer
int right = 0;
// final result
int res = 0;
/**
* NOTE !!! sliding window (while - while)
*
* https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/sliding_window.md
*/
while (right < s.length()) {
String cur = String.valueOf(s.charAt(right));
/**
* NOTE !!!
* Add element to map first (do val checks, index update below)
*/
if (map.containsKey(cur)){
map.put(cur, map.get(cur)+1);
}else{
map.put(cur, 1);
}
/**
* NOTE !!! if map has element -> duplicated element
* -> keep remove (while loop) count from element and move left pointer to right
* -> on the same time, left pointer (l) is moved to left
* -> and map is updated (remove element count)
*/
while (map.get(cur) > 1) {
String l = String.valueOf(s.charAt(left));
map.put(l, map.get(l) - 1);
left += 1;
}
res = Math.max(res, right - left + 1);
right += 1;
}
return res;
}
// V0'
// IDEA : SLIDING WINDOW + HASH SET
public int lengthOfLongestSubstring_(String s) {
if (s.equals("")){
return 0;
}
if (s.equals(" ")){
return 1;
}
if (s.length() == 1){
return 1;
}
int ans = 0;
char[] s_array = s.toCharArray();
for (int i = 0; i < s_array.length-1; i++){
int j = i;
Set<String> set = new HashSet<String>();
while (j < s_array.length){
String cur = String.valueOf(s_array[j]);
if (set.contains(cur)){
ans = Math.max(ans, set.size());
break;
}else{
set.add(cur);
ans = Math.max(ans, set.size());
j += 1;
}
}
}
return ans;
}
// V1
// IDEA : BRUTE FORCE
// https://leetcode.com/problems/longest-substring-without-repeating-characters/editorial/
public int lengthOfLongestSubstring_2(String s) {
int n = s.length();
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (checkRepetition(s, i, j)) {
res = Math.max(res, j - i + 1);
}
}
}
return res;
}
private boolean checkRepetition(String s, int start, int end) {
Set<Character> chars = new HashSet<>();
for (int i = start; i <= end; i++) {
char c = s.charAt(i);
if(chars.contains(c)){
return false;
}
chars.add(c);
}
return true;
}
// V2
// IDEA : SLIDING WINDOW
// https://leetcode.com/problems/longest-substring-without-repeating-characters/editorial/
public int lengthOfLongestSubstring_3(String s) {
Map<Character, Integer> chars = new HashMap();
int left = 0;
int right = 0;
int res = 0;
while (right < s.length()) {
char r = s.charAt(right);
chars.put(r, chars.getOrDefault(r,0) + 1);
while (chars.get(r) > 1) {
char l = s.charAt(left);
chars.put(l, chars.get(l) - 1);
left++;
}
res = Math.max(res, right - left + 1);
right++;
}
return res;
}
}