Skip to content

Commit ca00cad

Browse files
author
alxkm
committed
refactor: LongestNonRepetitiveSubstring
1 parent a9bc7c2 commit ca00cad

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,40 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* Class for finding the length of the longest substring without repeating characters.
8+
*/
69
final class LongestNonRepetitiveSubstring {
710
private LongestNonRepetitiveSubstring() {
811
}
912

13+
/**
14+
* Finds the length of the longest substring without repeating characters.
15+
*
16+
* @param s the input string
17+
* @return the length of the longest non-repetitive substring
18+
*/
1019
public static int lengthOfLongestSubstring(String s) {
11-
int max = 0;
20+
int maxLength = 0;
1221
int start = 0;
13-
int i = 0;
14-
Map<Character, Integer> map = new HashMap<>();
15-
16-
while (i < s.length()) {
17-
char temp = s.charAt(i);
18-
19-
// adding key to map if not present
20-
if (!map.containsKey(temp)) {
21-
map.put(temp, 0);
22-
} else if (s.charAt(start) == temp) {
23-
start++;
24-
} else if (s.charAt(i - 1) == temp) {
25-
if (max < map.size()) {
26-
max = map.size();
27-
}
28-
map = new HashMap<>();
29-
start = i;
30-
i--;
31-
} else {
32-
if (max < map.size()) {
33-
max = map.size();
34-
}
35-
while (s.charAt(start) != temp) {
36-
map.remove(s.charAt(start));
37-
start++;
38-
}
39-
start++;
22+
Map<Character, Integer> charIndexMap = new HashMap<>();
23+
24+
for (int i = 0; i < s.length(); i++) {
25+
char currentChar = s.charAt(i);
26+
27+
// If the character is already in the map and its index is within the current window
28+
if (charIndexMap.containsKey(currentChar) && charIndexMap.get(currentChar) >= start) {
29+
// Move the start to the position right after the last occurrence of the current character
30+
start = charIndexMap.get(currentChar) + 1;
4031
}
4132

42-
i++;
43-
}
44-
if (max < map.size()) {
45-
max = map.size();
33+
// Update the last seen index of the current character
34+
charIndexMap.put(currentChar, i);
35+
36+
// Calculate the maximum length of the substring without repeating characters
37+
maxLength = Math.max(maxLength, i - start + 1);
4638
}
47-
return max;
39+
40+
return maxLength;
4841
}
4942
}
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.thealgorithms.strings;
22

3-
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
59

610
public class LongestNonRepetitiveSubstringTest {
711

8-
@Test
9-
public void palindrome() {
10-
String input1 = "HelloWorld";
11-
String input2 = "javaIsAProgrammingLanguage";
12-
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input1), 5);
13-
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input2), 9);
12+
private static Stream<Arguments> provideTestCases() {
13+
return Stream.of(Arguments.of("", 0), Arguments.of("a", 1), Arguments.of("abcde", 5), Arguments.of("aaaaa", 1), Arguments.of("abca", 3), Arguments.of("abcdeabc", 5),
14+
Arguments.of("a1b2c3", 6), Arguments.of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 62), Arguments.of("aabb", 2), Arguments.of("abcdefghijabc", 10));
15+
}
16+
17+
@ParameterizedTest
18+
@MethodSource("provideTestCases")
19+
void testLengthOfLongestSubstring(String input, int expectedLength) {
20+
assertEquals(expectedLength, LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input));
1421
}
1522
}

0 commit comments

Comments
 (0)