class Solution:
def LCS(self , str1: str, str2: str) -> str:
if len(str1) < len(str2): # 从长度大的字符串开始遍历
str1, str2 = str2, str1
max_len = 0 # 记录最大子串的长度
res = ''
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

京公网安备 11010502036488号