diff --git a/src/LongestSubstringWithoutRepeatingCharacters3.java b/src/main/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3.java
similarity index 53%
rename from src/LongestSubstringWithoutRepeatingCharacters3.java
rename to src/main/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3.java
index f70d4f8..7ea4853 100644
--- a/src/LongestSubstringWithoutRepeatingCharacters3.java
+++ b/src/main/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3.java
@@ -1,16 +1,19 @@
-// Sliding Window, Hash Table, String
+// Tags: Sliding Window, HashSet, String
+package com.leetcode.medium;
import java.util.HashSet;
+import java.util.Set;
public class LongestSubstringWithoutRepeatingCharacters3 {
- /*
- Time complexity : O(2n) = O(n). In the worst case each character will be visited twice by l and r.
- Space complexity : O(n)
+ /**
+ * Time complexity : O(2n) = O(n). In the worst case each character will be visited twice by l and r.
+ *
+ * Space complexity : O(n)
*/
public int lengthOfLongestSubstring(String s) {
int res = 0;
int l = 0;
- HashSet hashSet = new HashSet<>();
+ Set hashSet = new HashSet<>();
for (int r = 0; r < s.length(); r++) {
while (hashSet.contains(s.charAt(r))) {
hashSet.remove(s.charAt(l));
@@ -21,9 +24,4 @@ public int lengthOfLongestSubstring(String s) {
}
return res;
}
-
- public static void main(String[] args) {
- String s = "abcabcbb";
- System.out.println(new LongestSubstringWithoutRepeatingCharacters3().lengthOfLongestSubstring(s));
- }
}
diff --git a/src/test/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3Test.java b/src/test/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3Test.java
new file mode 100644
index 0000000..4748efa
--- /dev/null
+++ b/src/test/java/com/leetcode/medium/LongestSubstringWithoutRepeatingCharacters3Test.java
@@ -0,0 +1,68 @@
+package com.leetcode.medium;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+
+public class LongestSubstringWithoutRepeatingCharacters3Test {
+ private LongestSubstringWithoutRepeatingCharacters3 solution;
+
+ @BeforeEach
+ void setUp() {
+ solution = new LongestSubstringWithoutRepeatingCharacters3();
+ }
+
+ @Test
+ void testLengthOfLongestSubstring_Example1() {
+ String s = "abcabcbb";
+ int expected = 3;
+
+ int result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.lengthOfLongestSubstring(s));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLengthOfLongestSubstring_Example2() {
+ String s = "bbbbb";
+ int expected = 1;
+
+ int result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.lengthOfLongestSubstring(s));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLengthOfLongestSubstring_Example3() {
+ String s = "pwwkew";
+ int expected = 3;
+
+ int result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.lengthOfLongestSubstring(s));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLengthOfLongestSubstring_Example4() {
+ String s = "tmmzuxt";
+ int expected = 5;
+
+ int result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.lengthOfLongestSubstring(s));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLengthOfLongestSubstring_Example5() {
+ String s = "aab";
+ int expected = 2;
+
+ int result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.lengthOfLongestSubstring(s));
+
+ assertEquals(expected, result);
+ }
+}