Skip to content

Commit c68a5b0

Browse files
author
eunhwa99
committed
longest subsring without repeating characters
1 parent fa625bd commit c68a5b0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
// 시간 복잡도: O(N)
3+
// 공간 복잡도: O(N)
4+
public int lengthOfLongestSubstring(String s) {
5+
Map<Character, Integer> position = new HashMap<>();
6+
int start = 0; // substring의 시작점
7+
int maxLength = 0;
8+
9+
for (int idx = 0; idx < s.length(); idx++) {
10+
char currentChar = s.charAt(idx);
11+
12+
// 같은 문자가 이미 map 에 있고, 그 문자가 현재 substring에 포함된 문자인지 확인
13+
if (position.containsKey(currentChar) && position.get(currentChar) >= start) {
14+
start = position.get(currentChar) + 1;
15+
// 같은 문자가 포함되지 않게 substring의 시작을 옮긴다.
16+
}
17+
18+
maxLength = Math.max(maxLength, idx - start + 1);
19+
20+
position.put(currentChar, idx);
21+
}
22+
23+
return maxLength;
24+
}
25+
}

0 commit comments

Comments
 (0)