refactor: fix typo in class name LongestNonRepetitiveSubstring (#5359)

This commit is contained in:
Alex Klymenko
2024-08-22 11:37:52 +02:00
committed by GitHub
parent 622a3bf795
commit 74b05ef7c2
2 changed files with 7 additions and 6 deletions

View File

@ -0,0 +1,49 @@
package com.thealgorithms.strings;
import java.util.HashMap;
import java.util.Map;
final class LongestNonRepetitiveSubstring {
private LongestNonRepetitiveSubstring() {
}
public static int lengthOfLongestSubstring(String s) {
int max = 0;
int start = 0;
int i = 0;
Map<Character, Integer> map = new HashMap<>();
while (i < s.length()) {
char temp = s.charAt(i);
// adding key to map if not present
if (!map.containsKey(temp)) {
map.put(temp, 0);
} else if (s.charAt(start) == temp) {
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++;
}
if (max < map.size()) {
max = map.size();
}
return max;
}
}