-
Notifications
You must be signed in to change notification settings - Fork 0
/
0005.LongestPalindromicSubstring.js
118 lines (87 loc) · 2.4 KB
/
0005.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Given a string s, return the longest palindromic substring in s.
//
// Example 1:
// Input: s = "babad"
// Output: "bab"
// Note: "aba" is also a valid answer.
// Example 2:
// Input: s = "cbbd"
// Output: "bb"
// Example 3:
// Input: s = "a"
// Output: "a"
// Example 4:
// Input: s = "ac"
// Output: "a"
//
// Constraints:
// 1 <= s.length <= 1000
// s consist of only digits and English letters.
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/longest-palindromic-substring
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 🎨 方法一:动态规划 - 求回文子串数
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
let n = s.length;
if (n === 0) {
return 0
}
let ans = 0;
// 定义dp[i][j]的值表示子串[i,j]是否为回文,并初始化
const dp = new Array(n).fill(false).map(() => new Array(n).fill(false))
// 自身是回文字符串
for (let i = 0; i < n; i++) {
dp[i][i] = true;
ans++
}
for (let j = 1; j < n; j++) {
for (let i = 0; i < j; i++) {
// 子串 [i,j] : j - i < 3 的三种情形:a aa aba
dp[i][j] = s[i] === s[j] && (j - i < 3 || dp[i + 1][j - 1])
if (dp[i][j]) {
ans++
}
}
}
return ans
};
// 🎨 方法一:动态规划 - 求最长回文子串
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
let n = s.length;
if (n === 0) {
return ''
}
let ans = '';
// 定义dp[i][j]的值表示子串[i,j]是否为回文,并初始化
const dp = new Array(n).fill(false).map(() => new Array(n).fill(false))
// 自身是回文字符串
for (let i = 0; i < n; i++) {
dp[i][i] = true;
ans++
}
for (let l = 0; l < n; l++) {
for (let i = 0; i + l < n; i++) {
let j = i + l
// [i,j]
if (l === 0) {
dp[i][j] = true; // a
} else if (l === 1) {
dp[i][j] = s[i] === s[j]; // aa
} else {
dp[i][j] = s[i] === s[j] && dp[i + 1][j - 1]; // aba ...
}
if (dp[i][j] && l + 1 > res.length) {
res = s.slice(i, i + l + 1)
}
}
}
return ans
};