Skip to content

Commit 9f46e09

Browse files
add a solution for 3
1 parent aed3eab commit 9f46e09

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/main/java/com/fishercoder/solutions/_3.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,44 @@ public int lengthOfLongestSubstring(String s) {
120120
return longest;
121121
}
122122
}
123+
124+
public static class Solution6 {
125+
/**
126+
* Sliding Window, my completely original idea on 10/20/2021. Although less efficient then Solution5, it follows a generic template without any manipulation.
127+
* Basically, keep moving the left boundary towards the right and keep updating the result along the way.
128+
* O(n) time
129+
* O(n) space
130+
*/
131+
132+
public int lengthOfLongestSubstring(String s) {
133+
int left = 0;
134+
int right = 0;
135+
int ans = 0;
136+
Map<Character, Integer> map = new HashMap<>();
137+
while (right < s.length()) {
138+
map.put(s.charAt(right), map.getOrDefault(s.charAt(right), 0) + 1);
139+
right++;
140+
if (allUnique(map)) {
141+
ans = Math.max(ans, right - left);
142+
}
143+
while (!allUnique(map)) {
144+
map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
145+
if (map.get(s.charAt(left)) == 0) {
146+
map.remove(s.charAt(left));
147+
}
148+
left++;
149+
}
150+
}
151+
return ans;
152+
}
153+
154+
private boolean allUnique(Map<Character, Integer> map) {
155+
for (char key : map.keySet()) {
156+
if (map.get(key) > 1) {
157+
return false;
158+
}
159+
}
160+
return true;
161+
}
162+
}
123163
}

src/test/java/com/fishercoder/_3Test.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class _3Test {
1212
private static _3.Solution3 solution3;
1313
private static _3.Solution4 solution4;
1414
private static _3.Solution5 solution5;
15+
private static _3.Solution6 solution6;
16+
private static int expected;
17+
private static String s;
1518

1619
@BeforeClass
1720
public static void setup() {
@@ -20,15 +23,19 @@ public static void setup() {
2023
solution3 = new _3.Solution3();
2124
solution4 = new _3.Solution4();
2225
solution5 = new _3.Solution5();
26+
solution6 = new _3.Solution6();
2327
}
2428

2529
@Test
2630
public void test1() {
27-
assertEquals(3, solution1.lengthOfLongestSubstring("abcabcbb"));
28-
assertEquals(3, solution2.lengthOfLongestSubstring("abcabcbb"));
29-
assertEquals(3, solution3.lengthOfLongestSubstring("abcabcbb"));
30-
assertEquals(3, solution4.lengthOfLongestSubstring("abcabcbb"));
31-
assertEquals(3, solution5.lengthOfLongestSubstring("abcabcbb"));
31+
expected = 3;
32+
s = "abcabcbb";
33+
assertEquals(expected, solution1.lengthOfLongestSubstring(s));
34+
assertEquals(expected, solution2.lengthOfLongestSubstring(s));
35+
assertEquals(expected, solution3.lengthOfLongestSubstring(s));
36+
assertEquals(expected, solution4.lengthOfLongestSubstring(s));
37+
assertEquals(expected, solution5.lengthOfLongestSubstring(s));
38+
assertEquals(expected, solution6.lengthOfLongestSubstring(s));
3239
}
3340

3441
}

0 commit comments

Comments
 (0)