Skip to content

Commit 913a63f

Browse files
committed
✨ [q3] New beat! 89.19%, 92.70%
1 parent 159eb59 commit 913a63f

File tree

2 files changed

+83
-122
lines changed

2 files changed

+83
-122
lines changed

3/my_solution.js

+72-89
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,82 @@
33
* @return {number}
44
*/
55
const lengthOfLongestSubstring = (s) => {
6-
// Put into a map {index: 0, char: "a", nbAppeared: 1}
76
let tmpMap = new Map(),
87
charList = s.split(''),
98
tmpList = [], // Deciding result
109
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 = [];
2011

2112
for (let i = 0; i < charList.length; i++) {
2213
// Might need extra handle if there is upper/lower case.
2314
let currChar = charList[i],
2415
existCharMap = tmpMap.get(currChar);
2516

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];
9120
}
9221

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)
9882

9983
result = prevTmpList.length > tmpList.length ? prevTmpList : tmpList;
10084
}
@@ -103,24 +87,23 @@ const lengthOfLongestSubstring = (s) => {
10387
console.log(tmpMap)
10488
console.log(result)
10589
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)
11396

11497
return result.length;
11598
};
11699

117100
// lengthOfLongestSubstring("abcabcbb") // abc
118101
// lengthOfLongestSubstring("bbbbb") // b
119-
// lengthOfLongestSubstring("pwwkew") // wke
102+
lengthOfLongestSubstring("pwwkew") // wke
120103
// lengthOfLongestSubstring("dvdf") // vdf
121104
// lengthOfLongestSubstring("aa") // 1
122105
// lengthOfLongestSubstring(" ") // 1
123106
// lengthOfLongestSubstring("au") // 2
124107
// lengthOfLongestSubstring("cdd") // 1
125108
// lengthOfLongestSubstring("anviaj") // 5
126-
lengthOfLongestSubstring("ckilbkd") // 5
109+
// lengthOfLongestSubstring("ckilbkd") // 5

3/solution.js

+11-33
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,23 @@
33
* @return {number}
44
*/
55
const lengthOfLongestSubstring = (s) => {
6-
// Put into a map {index: 0, char: "a", nbAppeared: 1}
7-
let tmpMap = new Map(),
8-
charList = s.split(''),
9-
tmpList = [], // Deciding result
10-
result = [];
11-
12-
var prevTmpList = [];
6+
let charList = s.split(''),
7+
tmpList = [],
8+
result = [],
9+
prevTmpList = [];
1310

1411
for (let i = 0; i < charList.length; i++) {
15-
// Might need extra handle if there is upper/lower case.
16-
let currChar = charList[i],
17-
existCharMap = tmpMap.get(currChar);
18-
19-
if (existCharMap) {
20-
if (tmpList.includes(currChar)) {
21-
if (tmpList.length > prevTmpList.length) prevTmpList = [...tmpList];
12+
let currChar = charList[i];
2213

23-
let nbRemove = tmpList.indexOf(currChar);
24-
for (let j = 0; j <= nbRemove; j++) {
25-
tmpList.shift();
26-
}
27-
tmpList.push(currChar);
14+
if (tmpList.includes(currChar) && tmpList.length > prevTmpList.length) prevTmpList = [...tmpList];
2815

29-
} else {
30-
for (let j = 0; j <= tmpList.indexOf(currChar); j++) {
31-
tmpList.shift();
32-
}
33-
tmpList.push(currChar);
34-
}
35-
36-
// Update values
37-
existCharMap.nbAppeared += 1;
38-
tmpMap.set(currChar, existCharMap);
39-
} else {
40-
// Set it into the map if doesnt exists
41-
tmpMap.set(currChar, { index: i, nbAppeared: 1 });
42-
tmpList.push(currChar)
16+
let nbRemove = tmpList.indexOf(currChar);
17+
for (let j = 0; j <= nbRemove; j++) {
18+
tmpList.shift();
4319
}
4420

21+
tmpList.push(currChar);
22+
4523
result = prevTmpList.length > tmpList.length ? prevTmpList : tmpList;
4624
}
4725

0 commit comments

Comments
 (0)