每日一题 【每日一题】最长公共子串-Python-20211126

Jack · 2021年11月26日 · 最后由 Jack 回复于 2021年11月27日 · 75 次阅读
本帖已被设为精华帖!

给出两个字符串,找到最长公共子串,返回其长度

Jack 将本帖设为了精华贴 11月26日 22:47

参考代码:

class Solution:
    def longSubstring(self, A, B):
        ans = 0
        for i in range(len(A)):
            for j in range(len(B)):
                l = 0
                while i + 1 < len(A) and j + 1 < len(B) and A[i+1] == B[j+1]:
                    l += 1
                if l > ans:
                    ans = 1
        return ans

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册