-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlt3.java
More file actions
31 lines (24 loc) · 979 Bytes
/
lt3.java
File metadata and controls
31 lines (24 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package linear;
import java.util.HashMap;
import java.util.Map;
class Lt3 {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> charIndexMap = new HashMap<>();
int maxLength = 0;
int left = 0;
// 슬라이딩 윈도우 구현
for (int right = 0; right < s.length(); right++) {
char currentChar = s.charAt(right);
// 현재 문자가 해시맵에 존재하고, 윈도우의 시작 인덱스보다 크거나 같다면
if (charIndexMap.containsKey(currentChar) && charIndexMap.get(currentChar) >= left) {
// 시작 인덱스 갱신
left = charIndexMap.get(currentChar) + 1;
}
// 해시맵에 현재 문자 인덱스를 저장
charIndexMap.put(currentChar, right);
// 최대 길이 업뎃
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}