代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

longest common substring

@param str1 string字符串 the string

@param str2 string字符串 the string

@return string字符串

动态规划, d[n] = d[n-1]+n

class Solution: def LCS(self , str1: str, str2: str) -> str: # write code here tmp_str, res = '','' for i in range(len(str1)): if tmp_str not in str2: # 记录当前最大长度子串 res = tmp_str[:-1] if len(tmp_str) > len(res) else res tmp_str = tmp_str[-2:] + str1[i] else: tmp_str += str1[i] # res = tmp_str if len(tmp_str) > len(res) else res return res

吐槽,编程10分钟,调边界2小时。