Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Space complexity : O(n)
*/
public int lengthOfLongestSubstring(String s) {
int res = 0;
int l = 0;
HashSet<Character> hashSet = new HashSet<>();
Set<Character> hashSet = new HashSet<>();
for (int r = 0; r < s.length(); r++) {
while (hashSet.contains(s.charAt(r))) {
hashSet.remove(s.charAt(l));
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}