【最长公共子序列】
class Solution(object):
def longestCommonSubsequence(self, text1, text2):
"""
:type text1: str
:type text2: str
:rtype: int
"""
m = len(text1)
n = len(text2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if text1[i-1] == text2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j],dp[i][j-1])
return dp[m][n]
【最长公共子串】
class Solution:
def LCS(self , str1: str, str2: str) -> str:
#让str1为较长的字符串
if len(str1) < len(str2):
str1, str2 = str2, str1
res = ''
max_len = 0
#遍历str1的长度
for i in range(len(str1)):
#查找是否存在
if str1[i - max_len : i + 1] in str2:
res = str1[i - max_len : i + 1]
max_len += 1
return res
class Solution:
def LCS(self , str1: str, str2: str) -> str:
# write code here
m = len(str1)
n = len(str2)
dp = [[0]*(n+1) for _ in range(m+1)]
z = 0
flag = 0
for i in range(m):
for j in range(n):
if str1[i] == str2[j]:
dp[i+1][j+1] = dp[i][j] + 1
if dp[i+1][j+1] >z:
z = dp[i+1][j+1]
flag = i+1
return str1[flag-z:flag]
【最长连续序列】
class Solution(object):
def longestConsecutive(self, nums):
hash_dict = dict()
max_length = 0
for num in nums:
if num not in hash_dict:
left = hash_dict.get(num - 1, 0)
right = hash_dict.get(num + 1, 0)
cur_length = 1 + left + right
if cur_length > max_length:
max_length = cur_length
hash_dict[num] = cur_length
hash_dict[num - left] = cur_length
hash_dict[num + right] = cur_length
return max_length
【最长递增子序列】
class Solution:
def LIS(self , arr: List[int]) -> int:
# write code here
dp = [1 for _ in range(len(arr))]
res = 0
for i in range(1,len(arr)):
for j in range(i):
if arr[i]>arr[j] and dp[i]<dp[j]+1:
dp[i] = dp[j] + 1
res = max(res,dp[i])
return res
【最长回文子序列】
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
dp = [[0] * len(s) for _ in range(len(s))]
for i in range(len(s)-1, -1, -1):
dp[i][i] = 1
for j in range(i + 1, len(s)):
if s[i] == s[j]:
dp[i][j] = dp[i+1][j-1] + 2
else:
dp[i][j] = max(dp[i+1][j], dp[i][j-1])
return dp[0][-1]
【最长回文子串】
class Solution:
def getLongestPalindrome(self , A: str) -> int:
# write code here
n = len(A)
if n < 2:
return len(A)
max_len = 1
begin = 0
# dp[i][j] 表示 s[i..j] 是否是回文串
dp = [[False] * n for _ in range(n)]
for i in range(n):
dp[i][i] = True
# 递推开始
# 先枚举子串长度
for L in range(2, n + 1):
# 枚举左边界,左边界的上限设置可以宽松一些
for i in range(n):
# 由 L 和 i 可以确定右边界,即 j - i + 1 = L 得
j = L + i - 1
# 如果右边界越界,就可以退出当前循环
if j >= n:
break
if A[i] != A[j]:
dp[i][j] = False
else:
if j - i < 3:
dp[i][j] = True
else:
dp[i][j] = dp[i + 1][j - 1]
# 只要 dp[i][L] == true 成立,就表示子串 s[i..L] 是回文,此时记录回文长度和起始位置
if dp[i][j] and j - i + 1 > max_len:
max_len = j - i + 1
begin = i
return max_len
【最长无重复子串】
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(s) <= 1: return len(s)
char_map, left, ans = dict(), -1, 0
for i, char in enumerate(s):
if char not in char_map:
char_map[char] = i
else:
left = max(left, char_map[char])
char_map[char] = i
ans = max(ans, i - left)
return ans
【最长无重复子数组】
class Solution:
def maxLength(self , arr: List[int]) -> int:
# write code here
dp = [0 for _ in range(len(arr)+1)]
from collections import defaultdict
temp = defaultdict(int)
for i in range(1,len(arr)+1):
dp[i] = min(i-temp[arr[i-1]],dp[i-1]+1)
temp[arr[i-1]] = i
return max(dp)
1312. 让字符串成为回文串的最少插入次数
【给你一个字符串 s ,每一次操作你都可以在字符串的任意位置插入任意字符。
请你返回让 s 成为回文串的 最少操作次数 。
「回文串」是正读和反读都相同的字符串。】
class Solution:
def minInsertions(self, s: str) -> int:
n = len(s)
dp = [[0]*n for i in range(n)]
for i in range(n-2, -1, -1):
for j in range(i+1, n):
if s[i] == s[j]:
dp[i][j] = dp[i+1][j-1]
else:
dp[i][j] = min(min(dp[i+1][j], dp[i][j-1])+1, dp[i+1][j-1]+2)
return dp[0][n-1]