【发布时间】:2019-10-14 09:58:26
【问题描述】:
任务是在最初的两个字符串中找到最长公共 substing 的索引,例如对于两个字符串'money','working for food not maney' 第一个字符串为 2,第二个字符串为 20。
获取最长子字符串的当前代码如下。如何修改下面的函数来获取初始字符串中最长子字符串的索引:
def get_len_long_substr(str1, str2):
substring = ''
len_str1 = len(str1)
if len_str1 > 0:
for i in range(len_str1):
for j in range(len_str1 - i + 1):
if j > len(substring) and all(str1[i:i + j] in x for x in [str1, str2]):
substring = str1[i:i + j]
return len(substring), substring
get_len_long_substr('money', 'working for food not maney')
3, 'ney'
# Target index is 2, 20
【问题讨论】:
标签: python arrays python-3.x string