-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-longest-palindrome-dp.js
58 lines (48 loc) · 1.14 KB
/
5-longest-palindrome-dp.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
52
53
54
55
56
57
58
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
//create 2d array
let str_len = s.length
let dp_arr = new Array(str_len)
for(i = 0; i < str_len;i++){
dp_arr[i] = new Array(str_len)
}
let max_str = 0;
if(s.length == 1){
return s;
}
if(s.length == 2){
if(s[0] == s[1]){
return s;
} else {
return s[0]
}
}
//fill all i==j to 1
for(i = 0; i < str_len; i++){
dp_arr[i][i] = 1
max_str = s[i]
}
//fill all i and i + 1
for(i = 0; i < str_len; i++){
if(s[i] == s[i+1]){
dp_arr[i][i+1] = 1
max_str = s.slice(i,i+2)
}
}
//loop for length start from 3
for(k = 3; k <= str_len ; k++ ){
for(i = 0; i < str_len - k + 1; ++i){
let j = k + i - 1
if(s[i]==s[j] && dp_arr[i+1][j-1] == 1){
dp_arr[i][j] = 1;
max_length = k
max_str = s.slice(i, j+1)
}
}
}
return max_str
}
console.log(longestPalindrome('aadfdjaa'))