【发布时间】:2013-09-13 23:20:17
【问题描述】:
我想比较 2 个字符串并保持匹配,在比较失败的地方分开。
所以如果我有 2 个字符串 -
string1 = apples
string2 = appleses
answer = apples
另一个例子,因为字符串可能有多个单词。
string1 = apple pie available
string2 = apple pies
answer = apple pie
我确信有一种简单的 Python 方法可以做到这一点,但我无法解决,感谢任何帮助和解释。
【问题讨论】:
-
如果
string1 = bapples和string2 = cappleses怎么办? -
如果需要通用前缀:
os.path.commonprefix(['apples', 'appleses']) -> 'apples'` -
还可以查看维基书籍上的算法实现:en.wikibooks.org/w/index.php?title=Algorithm_Implementation/…
-
题目内容与题目不符。描述的问题是最长公共前缀
标签: python string algorithm time-complexity dynamic-programming