题解 | #最长公共子串#
最长公共子串
https://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # longest common substring # @param str1 string字符串 the string # @param str2 string字符串 the string # @return string字符串 # class Solution: def LCS(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) top, digit = min(m, n), 0 while top // (10 ** digit) > 10: digit += 1 while digit >= 0: longest, index = 0, 0 for length in range(top, 0, -(10 ** digit)): for i in range(m - length + 1): if str1[i : i + length] in str2: longest = length index = i break if longest: break top = length + 10 ** digit digit -= 1 return str1[index : index + longest]