1.题目描述
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
给定一个字符串 s
,找到 s
中最长的回文子串。你可以假设 s
的最大长度为 1000。
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
2.Solutions
dp(i, j)
represents whether s(i ... j)
can form a palindromic substring, dp(i, j)
is true when s(i)
equals to s(j)
and s(i+1 ... j-1)
is a palindromic substring. When we found a palindrome, check if it’s the longest one. Time complexity O(n^2).
1 | public static String longestPalindrome(String s) { |