Skip to content

Commit 159eb59

Browse files
committed
✨ [q3] 3 attempt, FINALLY! 4hrs+ spent
1 parent bdfb350 commit 159eb59

File tree

4 files changed

+153
-14
lines changed

4 files changed

+153
-14
lines changed

3/my_solution.js

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
tmpList = [], // Deciding result
10+
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 = [];
20+
21+
for (let i = 0; i < charList.length; i++) {
22+
// Might need extra handle if there is upper/lower case.
23+
let currChar = charList[i],
24+
existCharMap = tmpMap.get(currChar);
25+
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)
91+
}
92+
93+
console.log("____________________ output each loop ____________________")
94+
console.log("tmpList")
95+
console.log(tmpList)
96+
console.log("prevTmpList")
97+
console.log(prevTmpList)
98+
99+
result = prevTmpList.length > tmpList.length ? prevTmpList : tmpList;
100+
}
101+
102+
console.log("final output:")
103+
console.log(tmpMap)
104+
console.log(result)
105+
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)
113+
114+
return result.length;
115+
};
116+
117+
// lengthOfLongestSubstring("abcabcbb") // abc
118+
// lengthOfLongestSubstring("bbbbb") // b
119+
// lengthOfLongestSubstring("pwwkew") // wke
120+
// lengthOfLongestSubstring("dvdf") // vdf
121+
// lengthOfLongestSubstring("aa") // 1
122+
// lengthOfLongestSubstring(" ") // 1
123+
// lengthOfLongestSubstring("au") // 2
124+
// lengthOfLongestSubstring("cdd") // 1
125+
// lengthOfLongestSubstring("anviaj") // 5
126+
lengthOfLongestSubstring("ckilbkd") // 5

3/solution.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,44 @@ const lengthOfLongestSubstring = (s) => {
66
// Put into a map {index: 0, char: "a", nbAppeared: 1}
77
let tmpMap = new Map(),
88
charList = s.split(''),
9-
tmpResult = [], // Deciding result
9+
tmpList = [], // Deciding result
1010
result = [];
1111

12+
var prevTmpList = [];
13+
1214
for (let i = 0; i < charList.length; i++) {
1315
// Might need extra handle if there is upper/lower case.
1416
let currChar = charList[i],
1517
existCharMap = tmpMap.get(currChar);
1618

1719
if (existCharMap) {
18-
tmpResult = [currChar]; // Reset the tmp result
20+
if (tmpList.includes(currChar)) {
21+
if (tmpList.length > prevTmpList.length) prevTmpList = [...tmpList];
22+
23+
let nbRemove = tmpList.indexOf(currChar);
24+
for (let j = 0; j <= nbRemove; j++) {
25+
tmpList.shift();
26+
}
27+
tmpList.push(currChar);
28+
29+
} else {
30+
for (let j = 0; j <= tmpList.indexOf(currChar); j++) {
31+
tmpList.shift();
32+
}
33+
tmpList.push(currChar);
34+
}
1935

2036
// Update values
2137
existCharMap.nbAppeared += 1;
2238
tmpMap.set(currChar, existCharMap);
2339
} else {
2440
// Set it into the map if doesnt exists
25-
tmpMap.set(currChar, { index: i, nbAppeared: 1 })
26-
tmpResult.push(currChar)
41+
tmpMap.set(currChar, { index: i, nbAppeared: 1 });
42+
tmpList.push(currChar)
2743
}
2844

29-
if (tmpResult.length > result.length) result = tmpResult;
45+
result = prevTmpList.length > tmpList.length ? prevTmpList : tmpList;
3046
}
3147

32-
33-
console.log("final output:")
34-
console.log(tmpMap)
35-
console.log(result.join(""))
36-
return result.join("");
48+
return result.length;
3749
};
38-
39-
// lengthOfLongestSubstring("abcabcbb") // abc
40-
// lengthOfLongestSubstring("bbbbb") // b
41-
// lengthOfLongestSubstring("pwwkew") // wke

5/yt_solution.js 5/o_solution.js

File renamed without changes.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
4. -
77
5. [Longest Palindromic Substring](./5/)
88

9+
---
10+
solution.js -- The solution I submitted.
11+
my_solution.js -- My attemp.
12+
o_solution.js -- Online/others' solution, not by me.
13+
914
---
1015
## Run for JS
1116
```sh

0 commit comments

Comments
 (0)