-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestPalindromicSubstring.js
79 lines (65 loc) · 1.86 KB
/
longestPalindromicSubstring.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* https://leetcode.com/problems/longest-palindromic-substring
* @param {string} s
* @return {string}
* DP solution
* Time complexity - O(N^2)
* Space complexity - O(N^2) - due to the matrix..
*/
function longestPalindrome(s) {
const n = s.length;
const dp = Array.from({ length: n }, () => Array(n).fill(0));
let ans = [0, 0];
for (let i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (let i = 0; i < n - 1; i++) {
if (s[i] === s[i + 1]) {
dp[i][i + 1] = 1;
ans = [i, i + 1];
}
}
for (let diff = 2; diff < n; diff++) {
for (let i = 0; i < n - diff; i++) {
const j = i + diff;
if (s[i] === s[j] && dp[i + 1][j - 1]) {
dp[i][j] = 1;
ans = [i, j];
}
}
}
const [i, j] = ans;
return s.slice(i, j + 1);
}
/*
* Middle check solution
* Time complexity - O(N^2)
* Space complexity - O(1)
*/
// function longestPalindrome(s) {
// function expand(i, j) {
// let left = i;
// let right = j;
// while (left >= 0 && right < s.length && s[left] === s[right]) {
// left--;
// right++;
// }
// return right - left - 1;
// }
// let ans = [0, 0];
// for (let i = 0; i < s.length; i++) {
// let oddLength = expand(i, i);
// if (oddLength > ans[1] - ans[0] + 1) {
// let dist = Math.floor(oddLength / 2);
// ans = [i - dist, i + dist];
// }
// let evenLength = expand(i, i + 1);
// if (evenLength > ans[1] - ans[0] + 1) {
// let dist = Math.floor(evenLength / 2) - 1;
// ans = [i - dist, i + 1 + dist];
// }
// }
// const [start, end] = ans;
// return s.slice(start, end + 1);
// }
module.exports = longestPalindrome;