-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5. LongestPalindromicSubstring.js
More file actions
39 lines (39 loc) · 1.27 KB
/
Copy path5. LongestPalindromicSubstring.js
File metadata and controls
39 lines (39 loc) · 1.27 KB
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
var longestPalindrome = function(s) {
let lastElement = s.length-1;
let tmpj = 0;
let longestPal = s[0];
for(let i = 0;i<s.length-1;i++){
let tmpi = i;
// console.log(i + " " + j)
for(let j = lastElement;j >= i;j--){
// console.log(i + " " + j)
if(s[i] === s[j]){
// console.log(i + " " + j)
if(tmpj < j){
// console.log("-" + tmpj + " " + j)
tmpj = j;
}
if(i === j || i+1 === j){
console.log(tmpi + " " + tmpj + " " + s.substring(tmpi, tmpj+1))
if(longestPal.length < s.substring(tmpi, tmpj+1).length){
longestPal = s.substring(tmpi, tmpj+1)
// console.log("in")
}
}
i++;
}
else if(s[i] !== s[j]){
// console.log("-" + i + " " + j + " " + tmpj)
if(tmpj !== 0)
j = tmpj
tmpj = 0
i = tmpi
// console.log("--" + i + " " + j + " " + tmpj)
}
}
i = tmpi
tmpj = 0
}
return longestPal
};
// console.log(longestPalindrome("aacabkacaa"))