mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
style: enable TypeName
(#5214)
* style: enable `TypeName` in checkstyle * style: enable `TypeName` in checkstyle * Update directory * style: use proper formatting --------- Co-authored-by: StarDxxx <StarDxxx@users.noreply.github.com> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
final class LongestNonRepeativeSubstring {
|
||||
private LongestNonRepeativeSubstring() {
|
||||
}
|
||||
|
||||
public static int lengthOfLongestSubstring(String s) {
|
||||
int max = 0;
|
||||
int start = 0;
|
||||
int i = 0;
|
||||
HashMap<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);
|
||||
// checking if the first value is the dublicate value
|
||||
else if (s.charAt(start) == temp)
|
||||
start++;
|
||||
// checking if the previous value is dublicate value
|
||||
else if (s.charAt(i - 1) == temp) {
|
||||
if (max < map.size()) max = map.size();
|
||||
map = new HashMap<>();
|
||||
start = i;
|
||||
i--;
|
||||
}
|
||||
// last possible place where dublicate value can be is between start and 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user