File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간복잡도: O(n)
2
+ // 공간복잡도: O(n)
3
+
4
+ /**
5
+ * @param {string } s
6
+ * @return {number }
7
+ */
8
+ var lengthOfLongestSubstring = function ( s ) {
9
+ let maxCount = 0 ;
10
+ const map = new Map ( )
11
+
12
+ let leftIdx = 0 ;
13
+ for ( let rightIdx = 0 ; rightIdx < s . length ; rightIdx ++ ) {
14
+ const char = s [ rightIdx ]
15
+ if ( map . has ( char ) && map . get ( char ) >= leftIdx ) leftIdx = map . get ( char ) + 1 ;
16
+ map . set ( char , rightIdx )
17
+ maxCount = Math . max ( maxCount , rightIdx - leftIdx + 1 )
18
+ }
19
+
20
+ return maxCount
21
+ } ;
22
+
23
+
24
+ console . log ( lengthOfLongestSubstring ( "abcabcbb" ) )
25
+ console . log ( lengthOfLongestSubstring ( "bbbbb" ) )
26
+ console . log ( lengthOfLongestSubstring ( "pwwkew" ) )
27
+ console . log ( lengthOfLongestSubstring ( "abba" ) )
28
+
You can’t perform that action at this time.
0 commit comments