简体   繁体   English

如何在 python 中找到两个字符串之间的最长交集?

[英]How to find longest intersection between two strings in python?

I'm trying to write a program that would find the longest intersection between two strings.我正在尝试编写一个程序来找到两个字符串之间最长的交集。 The conditions are:条件是:

  1. If there is no common character the program returns an empty chain.如果没有公共字符,程序将返回一个空链。
  2. If there are multiple substrings of common characters with the same length it should return whichever is the largest, for example, for "bbaacc" and "aabb" the repeating substrings are "aa" and "bb" but as "bb" > "aa", so the programs must return only "bb".如果有多个相同长度的公共字符子串,它应该返回最大的一个,例如,对于“bbaacc”和“aabb”,重复的子串是“aa”和“bb”,但作为“bb”>“aa” ",所以程序必须只返回 "bb"。
  3. Finally the program should return the longest common substring, for instance, for "programme" and "grammaire" the return should be "gramm" not "gramme".最后,程序应该返回最长的公共 substring,例如,对于“programme”和“grammaire”,返回应该是“gramm”而不是“gramme”。

My code has a problem with this last condition, how could I change it so it works as expected?我的代码在这最后一个条件下有问题,我该如何更改它以使其按预期工作?

def intersection(v, w):
    if not v or not w:
        return ""
    x, xs, y, ys = v[0], v[1:], w[0], w[1:]
    if x == y:
        return x + intersection(xs, ys)
    else:
        return max(intersection(v, ys), intersection(xs, w), key=len)

Driver:司机:

print(intersection('programme', 'grammaire'))

cant find the issue with your code, but i solved it like this找不到您的代码的问题,但我这样解决了

def longest_str_intersection(a: str, b: str):

    # identify all possible character sequences from str a
    seqs = []
    for pos1 in range(len(a)):
        for pos2 in range(len(a)):
            seqs.append(a[pos1:pos2+1])
        
    # remove empty sequences
    seqs = [seq for seq in seqs if seq != '']

    # find segments in str b
    max_len_match = 0
    max_match_sequence = ''
    for seq in seqs:
        if seq in b:
            if len(seq) > max_len_match:
                max_len_match = len(seq)
                max_match_sequence = seq

    return max_match_sequence


longest_str_intersection('programme', 'grammaire')
-> 'gramm'

also interested to see if you found a more elegant solution!也有兴趣看看您是否找到了更优雅的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 在两个字符串之间找到最长的公共 substring? - How to find the longest common substring between two strings using Python? 如何以可能使用库函数的pythonic方式找到python中两个字符串之间的最长公共后缀前缀? - How to find the longest common suffix prefix between two strings in python in a pythonic way possibly using library functions? Python 3.x-如何在两个字符串中查找字符集的交集 - Python 3.x - How to find the intersection of the sets of characters in two strings 两个字符串列表python之间的交集 - Intersection between two list of strings python 检查Python中两个字符串之间的交集 - Check intersection between two strings in python 在 python 中的两个字符串中查找单词的交集 - Find intersection of words in two strings in python 如何检查两个字符串是否在python中有交集? - how to check if two strings have intersection in python? 找到两个字符串之间交集的最佳方法是什么? - What's the best way to find the intersection between two strings? 在Python Pandas中查找两列的交集 - >字符串列表 - Find intersection of two columns in Python Pandas -> list of strings 如何在python中找到曲面和直线之间的交点? - How to find an intersection between a surface and a line in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM