Palindrome
5. 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.
Time O(n^2), Space O(1)
2 condition: symmetric or none-symmetric
class Solution:
def longestPalindrome(self, s: str) -> str:
ans = ''
for i in range(len(s)):
str1 = self.helper(s, i, i)
str2 = self.helper(s, i, i+1)
cand = str1 if len(str1) > len(str2) else str2
if len(cand) > len(ans):
ans = cand
return ans
def helper(self, s, l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l-=1
r+=1
return s[l+1:r]
647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
132. Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Sequence DP
State: f[i]代表前i項中最小可以分成幾個
Func: f[i]=min{f[j]+1}, j<i && String(j,i) is Palindrom
Init: f[i] = i
Ans: f[i]-1, n個切n-1刀
每次check valid palindrome 可以用 memo 紀錄著
267. Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
Take left half of palindrome and make a permutation
214. Shortest Palindrome
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.
516. Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Diagonal DP
DP[i][j] = string[i:j] 為最長palindrome 的長度
O(2*n) space
1312. Minimum Insertion Steps to Make a String Palindrome
Given a string s. In one step you can insert any character at any index of the string.
Return the minimum number of steps to make s palindrome.
1297. Maximum Number of Occurrences of a Substring
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to
maxLetters.The substring size must be between
minSizeandmaxSizeinclusive.
Last updated
Was this helpful?