-
Notifications
You must be signed in to change notification settings - Fork 0
/
long_polidromes
51 lines (45 loc) · 1.29 KB
/
long_polidromes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
let polidromes = '';
let i = 0;
console.log(`s.length-->${s.length}`);
let max = 0;
if (s.length === 1) {
polidromes = s;
} if (s.length === 2) {
if (s[0] === s[s.length - 1]) {
polidromes = s;
}
} else {
while (i < s.length) {
let newStr = s.slice(i);
let j = newStr.length;
let k = 0;
while (j > 1) {
let str = newStr.slice(k, j);
let isDiv = str.length % 2 === 0;
let halfIndex = Math.floor(str.length / 2);
let netxAfterHalf = halfIndex;
let rightHalf = str.slice(netxAfterHalf, j);
let leftHalf = reverse(str.slice(k, isDiv ? halfIndex : halfIndex + 1));
if (leftHalf === rightHalf) {
if (max < str.length) {
max = str.length;
polidromes = str;
}
} else {
console.log('not similar');
}
j -= 1;
}
i += 1;
}
}
return polidromes;
};
function reverse(s) {
return s.split("").reverse().join("");
}