Title description
Give you a string s and find the longest palindrome substring in s.
example
示例 1:
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd"
输出:"bb"
示例 3:
输入:s = "a"
输出:"a"
示例 4:
输入:s = "ac"
输出:"a"
Source: LeetCode
Link: https://leetcode-cn.com/problems/longest-palindromic-substring, the copyright belongs to Leetcode Network.
Ideas and answers
Brute force
Brute force cracking is to determine whether it is a palindrome for each substring in it.
Judge whether each character is a palindrome. For example, use cbac
judge. The left and right pointers are symmetrical. If they are equal, move to the middle and continue to judge. If they are not equal, return false directly.
public static String longestPalindrome(String s) {
if (s == null || s.length() == 0) {
return s;
}
String result = s.substring(0, 1);
for (int i=0; i < s.length() - 1; i++) {
for (int j = i + 1; j < s.length(); j++) {
if (judge(s, i, j) && j - i + 1 > result.length()) {
result = s.substring(i, j+1);
}
}
}
return result;
}
// 判断每个子串是不是回文
public static boolean judge(String source, int start, int end) {
// 对称轴对比
while (start <= end) {
if (source.charAt(start) != source.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
Brute-force cracking is too complex and will time out, so it is not recommended.
Center development method
The palindrome string is always symmetrical in the center. When the brute force method was used, the substrings were cut out and then judged. The palindrome can only be proved to be all symmetrical. In this way, many detours have been taken, as long as the last one is asymmetry. All previous efforts have been lost.
Conversely, we might as well try to expand on both sides at every point, so that as long as there is no match, we can stop in time.
It is worth noting that how to find the center of the center development method? How many centers are there in 3 characters?
There are five centers in total, some centers may be a gap between two characters, and some centers may be characters. So when designing, we use left
and right
represent two pointers:
left = right
: The center of symmetry is a characterleft + 1 = right
: The center of symmetry is the gap between two characters
The specific implementation is as follows:
class Solution {
// 开始下标
public static int start = -1;
// 最大长度
public static int maxLen= 0;
public String longestPalindrome(String s) {
start = -1;
maxLen = 0;
if(s==null||s.length()==0){
return "";
}
for(int i=0;i<s.length();i++){
// 以当前字符为对称轴
judge(s,i,i);
// 以当前字符和下一个字符的间隙为对称轴
judge(s,i,i+1);
}
if(start == -1){
return "";
}
return s.substring(start,start+maxLen);
}
public void judge(String s,int left,int right){
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){
left--;
right++;
}
int size = right-left-1;
if(size > maxLen){
maxLen = size;
start = left+1;
}
}
}
Dynamic programming
In fact, if a string is a palindrome, it is the same when it is read backwards. In other words, it and the reversed string actually match exactly, so if we use a string to reverse it Can the result be obtained by converting the string to the statistics and matching one by one?
The answer is yes! Assumption that the original string is s1
, the inverted string s2
, string length n
, we use an array nums[n][n]
number to matching records, nums[i][j]
expressed s1[i]
trailing character substring, and to s2[j]
character sub-string end , The maximum value of the matching characters of the two.
When
s1[i] == s2[j]
:- If
i == 0
orj == 0
:nums[i][j] = 1
- Otherwise
nums[i][j] = nums[i - 1][j - 1] + 1;
- If
- If
s1[i] != s2[j]
, thennums[i][j]=0
What I said earlier is actually the state transition expression, that is, how nums[i][j]
nums[i][j]
depends on nums[i - 1][j - 1]
matches the current character. If the current character does not match, it is directly assigned to 0. Only when the current character matches, the previous matching value nums[i - 1][j - 1]
.
Assuming babad
as an example:
Calculation of the last two lines:
The implemented code is as follows:
class Solution {
public static String longestPalindrome(String s) {
if (s == null || s.length() == 0) {
return "";
}
if (s.length() == 1) {
return s;
}
int len = s.length();
String s1 = new StringBuffer(s).reverse().toString();
int[][] nums = new int[len][len];
int end = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (s1.charAt(i) == s.charAt(j)) {
if (i == 0 || j == 0) {
nums[i][j] = 1;
} else {
nums[i][j] = nums[i - 1][j - 1] + 1;
}
}
if (nums[i][j] > max) {
if (len - i - 1 + nums[i][j] - 1 == j) {
end = j;
max = nums[i][j];
}
}
}
}
return s.substring(end - max+1, end+1);
}
}
About the Author
[Profile of the author] :
Qin Huai, [161606b0f0324c Qinhuai Grocery Store ], the road to technology is not at a time, the mountains are high and the rivers are long, even if it is slow, it will never stop. Personal writing direction: Java source code analysis,
JDBC
, Mybatis
, Spring
, redis
, distributed,
sword refers to Offer,
LeetCode
, do not like to write a series of articles about the party whistle seriously, do not like to write a series of articles , I cannot guarantee that what I have written is completely correct, but I guarantee that what I have written has been practiced or searched for information. I hope to correct any omissions or errors.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。