lintcode python— 最长公共字符串
2017-02-11 14:45
399 查看
最长公共字符串
问题描述:给出两个字符串,找到最长公共子串,并返回其长度。
代码如下:
class Solution:
def longestCommonSubstring(self, A, B):
l= []
if A == '' or B == '': #排除特殊情况
return '',0
for i in range(len(A)):
for j in range(len(B)):
s = []
k = i #索引替换
v = j
while(A[k] == B[v]):
s.append(A[k])
if k == (len(A) - 1) or v == (len(B) - 1):
break
k = k + 1 #索引不能随便变
v = v + 1
l.append(s) #保存匹配字串
l1 = []
for n in l:
l1.append(len(n)) #转化为长度列表并保存
x = l1.index(max(l1))
print l
return l1[x] #返回最大长度
问题描述:给出两个字符串,找到最长公共子串,并返回其长度。
代码如下:
class Solution:
def longestCommonSubstring(self, A, B):
l= []
if A == '' or B == '': #排除特殊情况
return '',0
for i in range(len(A)):
for j in range(len(B)):
s = []
k = i #索引替换
v = j
while(A[k] == B[v]):
s.append(A[k])
if k == (len(A) - 1) or v == (len(B) - 1):
break
k = k + 1 #索引不能随便变
v = v + 1
l.append(s) #保存匹配字串
l1 = []
for n in l:
l1.append(len(n)) #转化为长度列表并保存
x = l1.index(max(l1))
print l
return l1[x] #返回最大长度
相关文章推荐
- LintCode Python 79.最长公共子串
- lintcode python ——最长公共前缀(LCP)
- lintcode-最长公共前缀-78
- LintCode : 最长公共子串
- Lintcode最长公共子串
- lintcode :longest common substring 最长公共子串
- 连接两个字符串中的不同字符 lintcode python
- lintcode最长公共前缀
- LintCode Python 55.比较字符串
- lintcode之最长公共前缀
- 求两个字符串的最长公共子串 python实现
- 面试宝典_Python.常规算法.0002.输出任意两个字符串中最长公共子串?
- LintCode Python 78.公共子串
- LintCode : 最长回文子字符串 Longest Palindromic Substring
- lintcode ---- 最长公共前缀
- Lintcode—(5)最长公共子串
- python实现求字符串最长公共子串
- python实现求两个字符串的最长公共子串方法
- LintCode:79. 最长公共子串 VS 77. 最长公共子序列
- LintCode Python 171.乱序字符串