-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (42 loc) · 1.12 KB
/
index.js
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 T = getNewString(s);
let maxlen = 1;
let center = 1;
let i = 1;
while(i < T.length){
let j = maxlen;
for(;j >= 1; j--){
//console.log(i + '-' + j);
if(T[i-j] && T[i+j] && (T[i-j] === T[i+j])){
if(j === 1){
let k = maxlen +1;
//console.log('i:' + i + ' k:' + k);
//console.log(T);
while(T[i-k] && T[i+k] && (T[i-k] === T[i+k])){
maxlen = k;
center = i;
k++;
//console.log('maxlen' + maxlen);
}
}
}else{
j = 0;
}
}
i++
}
center = center/2;
maxlen = maxlen/2;
return s.slice(center-maxlen,center + maxlen);
};
function getNewString(s){
let t = '#';
for(var i = 0; i< s.length; i++){
t = t + s[i] + '#';
}
return t;
}