# coding:utf-8
def find_longest_sub(s):
if len(s) ==0:
return 0
charsIndex = {}
startIndex = -1 # 当前子串的开始位置
oriStartIndex = startIndex # 保存全局最长子串的开始位置
maxLen = 0
for index, c in enumerate(s):
# 当前字符串出现过
if(c in charsIndex):
oriIndex = charsIndex.get(c)
# 出现的位置比当前子串还后,说明毁掉了这个子串,要重新建一个子串了
if(oriIndex > startIndex):
print oriIndex, startIndex, charsIndex
startIndex = oriIndex # 新的候选子串更新到出现重复子串的这个位置
# 当前子串破纪录了,进行记录更新
if(index - startIndex > maxLen):
maxLen = index - startIndex
oriStartIndex = startIndex
charsIndex[c] =index
max_str = s[oriStartIndex + 1: oriStartIndex + maxLen + 1]
print s, max_str, maxLen
return maxLen
find_longest_sub("absd")
最长不重复子串
最新推荐文章于 2025-03-24 20:00:00 发布