-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPalindromicSubstring.java
More file actions
72 lines (65 loc) · 2.24 KB
/
LongestPalindromicSubstring.java
File metadata and controls
72 lines (65 loc) · 2.24 KB
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
package string.palindrome;
/**
* @author Yang
* @version 0.1 2018/06/28.
************************************************************************************************
* 5. Longest Palindromic Substring
* https://leetcode.com/problems/longest-palindromic-substring/
* https://leetcode.com/articles/longest-palindromic-substring/
************************************************************************************************
* Given a string s, find the longest palindromic substring in s. You may assume that the
* maximum length of s is 1000.
*
* Example 1:
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
* Example 2:
* Input: "cbbd"
* Output: "bb"
************************************************************************************************
*/
public class LongestPalindromicSubstring {
/**
* 中心扩展法
*/
public String longestPalindrome(String s) {
if (null == s) {
return null;
}
int start = 0;
int end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i+1);
int len = Math.max(len1, len2);
System.out.println(len);
if (len > end - start) {
start = i - (len-1)/2;
end = i + len/2 + 1;
}
}
return s.substring(start, end);
}
/**
* 返回字符串以left和right为中心的最长回文子字符串的长度
* right == left 或者 right == left + 1
*/
private int expandAroundCenter(String s, int left, int right) {
assert(left == right || left + 1 == right);
int l = left;
int r = right;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
l--;
r++;
}
return r - l - 1;
}
// =================================================================================
public static void main(String[] args) {
LongestPalindromicSubstring longestPalindromicSubstring = new LongestPalindromicSubstring();
// bab或者aba
System.out.println(longestPalindromicSubstring.longestPalindrome("babad") + " <---> bab");
}
}