Python - how to find all intersections of two strings?

前端 未结 6 1690
花落未央
花落未央 2021-02-04 18:21

How to find all intersections (also called the longest common substrings) of two strings and their positions in both strings?

For example, if S1=\"never\"

6条回答
  •  [愿得一人]
    2021-02-04 19:03

    def  IntersectStrings( first,  second):
    x = list(first)
    #print x
    y = list(second)
    lst1= []
    lst2= []
    for i in x:
        if i in y:
            lst1.append(i)
    lst2 = sorted(lst1) + []
       # This  above step is an optional if it is required to be sorted      alphabetically use this or else remove it
    return ''.join(lst2)
    
    print IntersectStrings('hello','mello' )
    

提交回复
热议问题