面试题:查找两个字符串的连续子串
2014-03-02 11:47
344 查看
def GetChild(data1,data2): maxLength=end=tempLength=0 tempData={} # 选择出比较长字符串 largest=data1 other=data2 if len(data2)>len(data1): largest=data2 other=data1 # 将比较长的字符串每个字符及位置存在字典中,便于查找 for i in range(len(largest)): if largest[i] not in tempData :tempData[largest[i]]=[] tempData[largest[i]].append(i) # 遍历较短字符串准备找相同字符串 for i in range(len(other)): # 如果较长字符串中没有就丢弃 if other[i] not in tempData:continue # 字符重复出现,其下标存在字典中List中 indexList=tempData[other[i]] # 对重复字符遍历比较,查找字串 for index in indexList: firsti=i+1 tempLength=1 j=index+1 # 字串查找 while firsti<len(other) and j<len(largest) and other[firsti]==largest[j]: tempLength+=1 firsti+=1 j+=1 # 如果本次子串最长纪录下来 if tempLength>maxLength: maxLength=tempLength end=j return largest[end-maxLength:end] print(GetChild('likeyooooooooooou','loooookyou')) print(GetChild('likeyou','lookyou'))
相关文章推荐
- 华为机试---查找两个字符串a,b中的最长公共子串
- 查找两个字符串a,b中的最长公共子串
- 所谓微软面试题的解答:求一个字符串中连续出现次数最多的子串
- 【华为oj】oj2034查找两个字符串a,b中的最长公共子串
- 查找字符串中最长的连续数字子串
- 查找两个字符串的最大相同子串
- 求两个字符串的最长的连续公共子串
- 所谓微软面试题的解答:求一个字符串中连续出现次数最多的子串
- c语言查找两个字符串中的公共子串
- 求两个字符串的最长的连续公共子串(按位异或法)
- 最新的微软面试题,题目:求一个字符串中连续出现次数最多的子串
- 华为机试在线训练-牛客网(30)查找两个字符串a,b中的最长公共子串
- [面试题] 查找最长不含相同字符的连续子串
- 华为OJ 公共字串计算&&查找两个字符串a,b中的最长公共子串
- 查找两个字符串a,b中的最长公共子串
- 字符串操作:两个字符串的最长连续公共子串
- 查找两个字符串中的最大公共子串
- [百度面试题]找出字符串中连续出现次数最多的子串
- 最新的微软面试题,题目:求一个字符串中连续出现次数最多的子串
- 求两个字符串的最长连续子串