File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ const lengthOfLongestSubstring = ( s ) => {
6
+ // Put into a map {index: 0, char: "a", nbAppeared: 1}
7
+ let tmpMap = new Map ( ) ,
8
+ charList = s . split ( '' ) ,
9
+ tmpResult = [ ] , // Deciding result
10
+ result = [ ] ;
11
+
12
+ for ( let i = 0 ; i < charList . length ; i ++ ) {
13
+ // Might need extra handle if there is upper/lower case.
14
+ let currChar = charList [ i ] ,
15
+ existCharMap = tmpMap . get ( currChar ) ;
16
+
17
+ if ( existCharMap ) {
18
+ tmpResult = [ currChar ] ; // Reset the tmp result
19
+
20
+ // Update values
21
+ existCharMap . nbAppeared += 1 ;
22
+ tmpMap . set ( currChar , existCharMap ) ;
23
+ } else {
24
+ // Set it into the map if doesnt exists
25
+ tmpMap . set ( currChar , { index : i , nbAppeared : 1 } )
26
+ tmpResult . push ( currChar )
27
+ }
28
+
29
+ if ( tmpResult . length > result . length ) result = tmpResult ;
30
+ }
31
+
32
+
33
+ console . log ( "final output:" )
34
+ console . log ( tmpMap )
35
+ console . log ( result . join ( "" ) )
36
+ return result . join ( "" ) ;
37
+ } ;
38
+
39
+ // lengthOfLongestSubstring("abcabcbb") // abc
40
+ // lengthOfLongestSubstring("bbbbb") // b
41
+ // lengthOfLongestSubstring("pwwkew") // wke
You can’t perform that action at this time.
0 commit comments