https://leetcode.com/problems/longest-substring-without-repeating-characters/
중복 문자 없는 최장 substring 의 길이
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int right = 0;
int maxLength = 0;
for (int left = 0; left < s.length(); left++) {
while (right < s.length() && map.getOrDefault(s.charAt(right), 0) == 0) {
map.put(s.charAt(right), 1);
right++;
}
maxLength = Math.max(maxLength, right - left);
map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
}
return maxLength;
}
Sliding window는 보통 right 을 이동한다구 하네여 기억~~
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> lastSeen = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (lastSeen.containsKey(c)) {
left = Math.max(left, lastSeen.get(c) + 1);
}
lastSeen.put(c, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}