我使用这个函数来获取两个字符串中最长的公共(public)前缀,我使用 python 2.7.13 版本,但该函数只给我 0 索引。
def longestcommonPrefix(s1, s2):
i = 0
while i < len(s1) and i < len(s2) and s1[i] == s2[i]:
i += 1
return s1[:i]
longestcommonPrefix("ACCATGT", "ACCAGAC")
最佳答案
另一个解决方案解决了您的语法/缩进问题。另请注意,此功能已存在于标准库中:
from os.path import commonprefix
res = commonprefix(["ACCATGT", "ACCAGAC"]) # 'ACCA'
关于python - 最长公共(public)前缀仅在字符串中给出 0 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53651690/