Skip to content

Commit d35eba1

Browse files
committed
✨ [q5] Added solution from yt
1 parent e99a14c commit d35eba1

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
lines changed

5/solution_0.js 5/my_solution.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,59 @@
33
* @return {string}
44
*/
55
const longestPalindrome = (s) => {
6-
original_s = s;
6+
if (1 === s.length) return s;
7+
78
s = reformat(s);
89

9-
let l = Array(s.length).fill(0), highest_length = 0, maxRight = 0, center = 0;
10+
let tmp_count = 0, highest_length = 0, maxRight = 0, center = 0;
1011

11-
console.log(`s: ${s}, l: ${l}, h: ${highest_length}, center: ${center}, right: ${maxRight}, sl: ${s.length}`);
12+
console.log(`s: ${s}, h: ${highest_length}, center: ${center}, right: ${maxRight}, sl: ${s.length}`);
1213

1314
for (let i = 0; i < s.length; i++) {
1415
// left = (i * 2) - i;
1516
// console.log("___ : " + s[left] + ", i: " + i + ", left: " + left)
17+
console.log("_________________________________")
1618
console.log("----> i: " + i + ", s[i]: " + s[i])
17-
// left
19+
20+
let new_i = i;
1821
for (let j = 0; j < i; j++) {
1922
console.log("---> i: " + i + ", j: " + j)
2023
left = s[i - (j + 1)];
2124
right = s[i + (j + 1)];
2225
console.log("left: (" + (i - (j + 1)) + ") - " + left)
2326
console.log("right: (" + (i + (j + 1)) + ") - " + right)
2427

28+
// if (i < maxRight) ...
29+
2530
if (left !== right) {
26-
console.log("break bc the first neighbour already unmatched")
31+
console.log("break when the neighbour is unmatched")
2732
break;
2833
}
2934

3035
console.log("ctn ...")
31-
// console.log(l[i])
3236

33-
l[i] = l[i] + 1;
34-
if (l[i] > highest_length) {
35-
highest_length = l[i];
37+
tmp_count++;
38+
39+
if (tmp_count > highest_length) {
40+
console.log("--------> maybe highest: " + tmp_count)
41+
highest_length = tmp_count;
3642
center = i;
43+
maxRight = i + tmp_count;
44+
console.log("--------> maxRight: " + maxRight)
45+
// new_i = i + (tmp_count - 1);
46+
// console.log("new_i: " + i)
3747
}
38-
39-
console.log(l[i])
4048
}
49+
// i = new_i;
50+
// console.log("i += tmp_count: " + (i));
4151

52+
tmp_count = 0;
4253
}
4354

44-
console.log("finally list of match count: " + l)
4555
console.log("highest_length: " + highest_length)
4656
console.log("center: " + center)
4757
s_part = s.substring((center - highest_length), (center + highest_length));
58+
console.log("s_part: "+s_part)
4859
answer = s_part.replace(/#/g, "");
4960
console.log(answer);
5061
return answer;
@@ -62,4 +73,6 @@ const reformat = (s) => {
6273
}
6374

6475
longestPalindrome("NBNNBR")
76+
// longestPalindrome("cbbd")
77+
// longestPalindrome("ccc")
6578
// longestPalindrome("qweeeewqwe")

5/solution.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ const longestPalindrome = (s) => {
2828
};
2929

3030
const reformat = (s) => {
31-
s = s.split('');
32-
let rs = "";
31+
let rs = "#";
3332

34-
for (let i = 0; i < s.length; i++) {
35-
rs += `#${s[i]}`;
33+
for (let char of s) {
34+
rs += `${char}#`;
3635
}
3736

38-
return `${rs}#`;
37+
return rs;
3938
}

5/yt_solution.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const longestPalindrome = (s) => {
6+
let newString = reformat(s),
7+
P = Array(newString.length).fill(0),
8+
center = 0,
9+
rightBoundry = 0;
10+
11+
for (let i = 0; i < newString.length; i++) {
12+
let mirrorIndex = center - (i - center);
13+
// console.log("mirrorIndex: " + mirrorIndex)
14+
15+
if (i < rightBoundry) {
16+
P[i] = Math.min(rightBoundry - i, P[mirrorIndex])
17+
console.log(newString[i])
18+
console.log(`i < rightBoundry - P[${i}]: ` + P[i])
19+
}
20+
let left = i - (P[i] + 1);
21+
let right = i + (P[i] + 1);
22+
console.log(`P[i]: ${P[i] + 1}, left: ${left}, right: ${right}`)
23+
24+
while (right < newString.length && left >= 0 && newString[left] === newString[right]) {
25+
P[i]++;
26+
left--;
27+
right++;
28+
}
29+
30+
if (i + P[i] > rightBoundry) {
31+
center = i;
32+
rightBoundry = i + P[i];
33+
}
34+
35+
}
36+
37+
// console.log(Math.max(...P))
38+
// console.log(P.indexOf())
39+
40+
let
41+
max_length = Math.max(...P),
42+
loc = P.indexOf(Math.max(...P));
43+
// console.log(newString.substring((loc - max_length), (loc + max_length)).split("#").join(""))
44+
45+
return newString.substring((loc - max_length), (loc + max_length)).split("#").join("");
46+
};
47+
48+
const reformat = (s) => {
49+
s = s.split('');
50+
let rs = "";
51+
52+
for (let i = 0; i < s.length; i++) {
53+
rs += `#${s[i]}`;
54+
}
55+
56+
return `${rs}#`;
57+
}
58+
59+
// longestPalindrome("abbc")
60+
longestPalindrome("NBNNBR")

0 commit comments

Comments
 (0)