5. Longest Palindromic Substring[M]

https://leetcode.com/problems/longest-palindromic-substring/

Description

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

1
2
3
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

1
2
Input: "cbbd"
Output: "bb"

Solution

https://leetcode.com/problems/longest-palindromic-substring/solution/

有什么浅显易懂的Manacher Algorithm讲解?

http://en.wikipedia.org/wiki/Longest_palindromic_substring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def longestPalindrome(self, s: str) -> str:
# Transform S into T.
# For example, S = "abba", T = "^#a#b#b#a#$".
# ^ and $ signs are sentinels appended to each end to avoid bounds checking
T = '#'.join('^{}$'.format(s))
n = len(T)
P = [0] * n
C = R = 0
for i in range (1, n-1):
P[i] = (R > i) and min(R - i, P[2*C - i]) # equals to i' = C - (i-C)
# Attempt to expand palindrome centered at i
while T[i + 1 + P[i]] == T[i - 1 - P[i]]:
P[i] += 1

# If palindrome centered at i expand past R,
# adjust center based on expanded palindrome.
if i + P[i] > R:
C, R = i, i + P[i]

# Find the maximum element in P.
maxLen, centerIndex = max((n, i) for i, n in enumerate(P))
return s[(centerIndex - maxLen)//2: (centerIndex + maxLen)//2]

214. Shortest Palindrome[H]

https://leetcode.com/problems/shortest-palindrome/

Description

Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

Example 1:

1
2
Input: "aacecaaa"
Output: "aaacecaaa"

Example 2:

1
2
Input: "abcd"
Output: "dcbabcd"

Solution

https://leetcode.com/problems/shortest-palindrome/solution/

https://leetcode.com/problems/shortest-palindrome/discuss/60250/My-recursive-Python-solution

1
2
3
4
5
6
7
8
9
class Solution:
def shortestPalindrome(self, s: str) -> str:
if not s or len(s) == 1:
return s
j = 0
for i in reversed(range(len(s))):
if s[i] == s[j]:
j += 1
return s[::-1][:len(s)-j] + self.shortestPalindrome(s[:j-len(s)]) + s[j-len(s):]