简体   繁体   中英

greatest common divisor for strings in python

I would like to have a python function that given the list

mystrings = ['abcde', 'abcdf', 'abcef', 'abcnn']

returns the string 'abc', ie, the longest piece contained by all the elements in the list. I have a solution which just loops through the slices of mystring[0] , and compares it to the rest, and breaks out of the loop, whenever the first unmatching substring is found. However, I suspect that there must be a more efficient, elegant, and pythonic way of doing this.

Could someone point out how to do this properly?

The way you described, you want the biggest substring at the starting point:

>>> os.path.commonprefix(['abcde', 'abcdf', 'abcef', 'abcnn'])
'abc'

Once you realize that "Longest Common Substring" is the problem you're describing, it's easy to find what you want:

eg, from Wikibooks :

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM