-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.longest-palindromic-substring.ts
69 lines (59 loc) · 1.8 KB
/
5.longest-palindromic-substring.ts
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
function longestPalindrome(s: string): string {
let result = "";
let maxLength = 0;
for (let i = 0; i < s.length; i++) {
let left, right;
for (
left = i, right = i;
left >= 0 && right < s.length && s[left] === s[right];
left -= 1, right += 1
) {
if (right - left + 1 > maxLength) {
result = s.slice(left, right + 1);
maxLength = right - left + 1;
}
}
for (
left = i, right = i + 1;
left >= 0 && right < s.length && s[left] === s[right];
left -= 1, right += 1
) {
if (right - left + 1 > maxLength) {
result = s.slice(left, right + 1);
maxLength = right - left + 1;
}
}
}
return result;
}
// s[i] == s[j] && dp[i+1][j-1] = true => dp[i][j] = true
function longestPalindrome2(s: string): string {
const dp: boolean[][] = [];
let mi = 0;
let mj = 0;
let maxLength = 0;
for (let i = 0; i < s.length; i++) {
dp[i] = [];
dp[i][i] = true;
}
for (let i = s.length - 1; i >= 0; i--) {
for (let j = i + 1; j < s.length; j++) {
// The condition j - i === 1 is used to handle the base case where the substring s[i...j] is of length 2.
// In this case, we don't have a dp[i + 1][j - 1] to check because there are no characters between s[i] and s[j].
// So, we directly check if s[i] is the same as s[j] to determine if the substring s[i...j] is a palindrome.
if (j - i === 1 || dp[i + 1][j - 1]) {
dp[i][j] = s[i] === s[j];
}
if (dp[i][j] && j - i + 1 > maxLength) {
mi = i;
mj = j;
maxLength = j - i + 1;
}
}
}
return s.slice(mi, mj + 1);
}
console.log(longestPalindrome("babad"));
console.log(longestPalindrome("cbbd"));
console.log(longestPalindrome2("aaabcbaba"));
console.log(longestPalindrome2("cbbd"));