3
3
* @return {number }
4
4
*/
5
5
const lengthOfLongestSubstring = ( s ) => {
6
- // Put into a map {index: 0, char: "a", nbAppeared: 1}
7
6
let tmpMap = new Map ( ) ,
8
7
charList = s . split ( '' ) ,
9
8
tmpList = [ ] , // Deciding result
10
9
result = [ ] ,
11
- prevResult ;
12
-
13
- console . log ( "charList" )
14
- console . log ( charList )
15
- console . log ( charList . length )
16
-
17
- // if (1 >= charList.length) return charList.length;
18
-
19
- var prevTmpList = [ ] ;
10
+ prevTmpList = [ ] ;
20
11
21
12
for ( let i = 0 ; i < charList . length ; i ++ ) {
22
13
// Might need extra handle if there is upper/lower case.
23
14
let currChar = charList [ i ] ,
24
15
existCharMap = tmpMap . get ( currChar ) ;
25
16
26
- console . log ( `________________ i: ${ i } , currChar: ${ currChar } ,` ) ;
27
-
28
- if ( existCharMap ) {
29
- // Reset the tmp result 2nd time onwards
30
- console . log ( "yes existCharMap" )
31
-
32
- console . log ( "existCharMap" )
33
- console . log ( existCharMap )
34
-
35
- // if ((existCharMap.index + 1) === i) {
36
- // if (tmpList[0] === currChar) {
37
- // console.log("shifting tmpList")
38
- // tmpList.shift();
39
- // tmpList.push(currChar);
40
- // console.log(tmpList)
41
- // } else {
42
-
43
- console . log ( "tmpList.indexOf(currChar)" )
44
- console . log ( tmpList . indexOf ( currChar ) )
45
- if ( tmpList . includes ( currChar ) ) {
46
- console . log ( "------------- existing char, new tmp!!!" ) ;
47
-
48
- console . log ( prevTmpList )
49
- console . log ( tmpList )
50
-
51
- if ( tmpList . length > prevTmpList . length ) {
52
- console . log ( "------------- replacing bc >" ) ;
53
- prevTmpList = [ ...tmpList ] ;
54
- } else {
55
- console . log ( "------------- idc" ) ;
56
-
57
- }
58
-
59
- let nbRemove = tmpList . indexOf ( currChar ) ;
60
- for ( let j = 0 ; j <= nbRemove ; j ++ ) {
61
- console . log ( "shift loop" )
62
- tmpList . shift ( ) ;
63
- }
64
- tmpList . push ( currChar ) ;
65
-
66
- console . log ( "QHIUSJHHJDSAKHJDKJHASDKJHDKJSAD" )
67
- console . log ( prevTmpList )
68
- console . log ( tmpList )
69
-
70
- // if (tmpList[tmpList.length - 1] == currChar) {
71
- // tmpList = [currChar]
72
- // } else {
73
- // tmpList = [tmpList[tmpList.length - 1], currChar]; // new list
74
- // }
75
- } else {
76
- for ( let j = 0 ; j <= tmpList . indexOf ( currChar ) ; j ++ ) {
77
- console . log ( "shift loop" )
78
- tmpList . shift ( ) ;
79
- }
80
- tmpList . push ( currChar ) ;
81
- }
82
-
83
- // Update values
84
- existCharMap . nbAppeared += 1 ;
85
- tmpMap . set ( currChar , existCharMap ) ;
86
- // }
87
- } else {
88
- // Set it into the map if doesnt exists
89
- tmpMap . set ( currChar , { index : i , nbAppeared : 1 } ) ;
90
- tmpList . push ( currChar )
17
+ if ( tmpList . includes ( currChar ) && tmpList . length > prevTmpList . length ) {
18
+ console . log ( "------------- replacing bc >" ) ;
19
+ prevTmpList = [ ...tmpList ] ;
91
20
}
92
21
93
- console . log ( "____________________ output each loop ____________________" )
94
- console . log ( "tmpList" )
95
- console . log ( tmpList )
96
- console . log ( "prevTmpList" )
97
- console . log ( prevTmpList )
22
+ let nbRemove = tmpList . indexOf ( currChar ) ;
23
+ for ( let j = 0 ; j <= nbRemove ; j ++ ) {
24
+ tmpList . shift ( ) ;
25
+ }
26
+ tmpList . push ( currChar ) ;
27
+
28
+ // if (existCharMap) {
29
+ // if (tmpList.includes(currChar)) {
30
+ // console.log("------------- existing char, new tmp!!!");
31
+ //
32
+ // // console.log(prevTmpList)
33
+ // // console.log(tmpList)
34
+ //
35
+ // if (tmpList.length > prevTmpList.length) {
36
+ // console.log("------------- replacing bc >");
37
+ // prevTmpList = [...tmpList];
38
+ // } else {
39
+ // console.log("------------- idc");
40
+ //
41
+ // }
42
+ //
43
+ // let nbRemove = tmpList.indexOf(currChar);
44
+ // for (let j = 0; j <= nbRemove; j++) {
45
+ // console.log("shift loop")
46
+ // tmpList.shift();
47
+ // }
48
+ // tmpList.push(currChar);
49
+ //
50
+ // // console.log("QHIUSJHHJDSAKHJDKJHASDKJHDKJSAD")
51
+ // // console.log(prevTmpList)
52
+ // // console.log(tmpList)
53
+ //
54
+ // // if (tmpList[tmpList.length - 1] == currChar) {
55
+ // // tmpList = [currChar]
56
+ // // } else {
57
+ // // tmpList = [tmpList[tmpList.length - 1], currChar]; // new list
58
+ // // }
59
+ // } else {
60
+ // for (let j = 0; j <= tmpList.indexOf(currChar); j++) {
61
+ // console.log("shift loop")
62
+ // tmpList.shift();
63
+ // }
64
+ // tmpList.push(currChar);
65
+ // }
66
+ //
67
+ // // Update values
68
+ // existCharMap.nbAppeared += 1;
69
+ // tmpMap.set(currChar, existCharMap);
70
+ // // }
71
+ // } else {
72
+ // // Set it into the map if doesnt exists
73
+ // tmpMap.set(currChar, { index: i, nbAppeared: 1 });
74
+ // tmpList.push(currChar)
75
+ // }
76
+
77
+ // console.log("____________________ output each loop ____________________")
78
+ // console.log("tmpList")
79
+ // console.log(tmpList)
80
+ // console.log("prevTmpList")
81
+ // console.log(prevTmpList)
98
82
99
83
result = prevTmpList . length > tmpList . length ? prevTmpList : tmpList ;
100
84
}
@@ -103,24 +87,23 @@ const lengthOfLongestSubstring = (s) => {
103
87
console . log ( tmpMap )
104
88
console . log ( result )
105
89
console . log ( result . length )
106
- console . log ( "charList output:" )
107
- console . log ( charList . length )
108
- console . log ( charList [ 0 ] )
109
- console . log ( charList [ 1 ] )
110
- console . log ( charList [ 0 ] !== charList [ 1 ] )
111
-
112
- console . log ( result . length )
90
+ // console.log("charList output:")
91
+ // console.log(charList.length)
92
+ // console.log(charList[0])
93
+ // console.log(charList[1])
94
+ // console.log(charList[0] !== charList[1])
95
+ // console.log(result.length)
113
96
114
97
return result . length ;
115
98
} ;
116
99
117
100
// lengthOfLongestSubstring("abcabcbb") // abc
118
101
// lengthOfLongestSubstring("bbbbb") // b
119
- // lengthOfLongestSubstring("pwwkew") // wke
102
+ lengthOfLongestSubstring ( "pwwkew" ) // wke
120
103
// lengthOfLongestSubstring("dvdf") // vdf
121
104
// lengthOfLongestSubstring("aa") // 1
122
105
// lengthOfLongestSubstring(" ") // 1
123
106
// lengthOfLongestSubstring("au") // 2
124
107
// lengthOfLongestSubstring("cdd") // 1
125
108
// lengthOfLongestSubstring("anviaj") // 5
126
- lengthOfLongestSubstring ( "ckilbkd" ) // 5
109
+ // lengthOfLongestSubstring("ckilbkd") // 5
0 commit comments