File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments