面试题:查找两个字符串的连续子串

 

 

 

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 firstimaxLength:
                maxLength=tempLength
                end=j
    return largest[end-maxLength:end]

print(GetChild('likeyooooooooooou','loooookyou'))
print(GetChild('likeyou','lookyou'))


 

你可能感兴趣的:(算法面试题)