How to find the overlap between 2 sequences, and return it

前端 未结 4 2079
逝去的感伤
逝去的感伤 2020-12-02 23:45

I am new in Python, and have already spend to many hours with this problem, hope somebody can help me. I need to find the overlap between 2 sequences. The overlap is in the

4条回答
  •  醉酒成梦
    2020-12-03 00:31

    Longest Common Substring

    def LongestCommonSubstring(S1, S2):
      M = [[0]*(1+len(S2)) for i in xrange(1+len(S1))]
      longest, x_longest = 0, 0
      for x in xrange(1,1+len(S1)):
        for y in xrange(1,1+len(S2)):
            if S1[x-1] == S2[y-1]:
                M[x][y] = M[x-1][y-1] + 1
                if M[x][y]>longest:
                    longest = M[x][y]
                    x_longest  = x
            else:
                M[x][y] = 0
      return S1[x_longest-longest: x_longest]
    

提交回复
热议问题