refactor: LongestNonRepetitiveSubstring (#5421)

* refactor: LongestNonRepetitiveSubstring

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-28 18:40:27 +02:00 committed by GitHub
parent c413f3c6b2
commit b2815db5cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 39 deletions

View File

@ -3,47 +3,40 @@ package com.thealgorithms.strings;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* Class for finding the length of the longest substring without repeating characters.
*/
final class LongestNonRepetitiveSubstring { final class LongestNonRepetitiveSubstring {
private LongestNonRepetitiveSubstring() { private LongestNonRepetitiveSubstring() {
} }
/**
* Finds the length of the longest substring without repeating characters.
*
* @param s the input string
* @return the length of the longest non-repetitive substring
*/
public static int lengthOfLongestSubstring(String s) { public static int lengthOfLongestSubstring(String s) {
int max = 0; int maxLength = 0;
int start = 0; int start = 0;
int i = 0; Map<Character, Integer> charIndexMap = new HashMap<>();
Map<Character, Integer> map = new HashMap<>();
while (i < s.length()) { for (int i = 0; i < s.length(); i++) {
char temp = s.charAt(i); char currentChar = s.charAt(i);
// adding key to map if not present // If the character is already in the map and its index is within the current window
if (!map.containsKey(temp)) { if (charIndexMap.containsKey(currentChar) && charIndexMap.get(currentChar) >= start) {
map.put(temp, 0); // Move the start to the position right after the last occurrence of the current character
} else if (s.charAt(start) == temp) { start = charIndexMap.get(currentChar) + 1;
start++;
} else if (s.charAt(i - 1) == temp) {
if (max < map.size()) {
max = map.size();
}
map = new HashMap<>();
start = i;
i--;
} else {
if (max < map.size()) {
max = map.size();
}
while (s.charAt(start) != temp) {
map.remove(s.charAt(start));
start++;
}
start++;
} }
i++; // Update the last seen index of the current character
charIndexMap.put(currentChar, i);
// Calculate the maximum length of the substring without repeating characters
maxLength = Math.max(maxLength, i - start + 1);
} }
if (max < map.size()) {
max = map.size(); return maxLength;
}
return max;
} }
} }

View File

@ -1,15 +1,22 @@
package com.thealgorithms.strings; package com.thealgorithms.strings;
import org.junit.jupiter.api.Assertions; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class LongestNonRepetitiveSubstringTest { public class LongestNonRepetitiveSubstringTest {
@Test private static Stream<Arguments> provideTestCases() {
public void palindrome() { 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), Arguments.of("a1b2c3", 6), Arguments.of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 62),
String input1 = "HelloWorld"; Arguments.of("aabb", 2), Arguments.of("abcdefghijabc", 10));
String input2 = "javaIsAProgrammingLanguage"; }
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input1), 5);
Assertions.assertEquals(LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input2), 9); @ParameterizedTest
@MethodSource("provideTestCases")
void testLengthOfLongestSubstring(String input, int expectedLength) {
assertEquals(expectedLength, LongestNonRepetitiveSubstring.lengthOfLongestSubstring(input));
} }
} }