寻找字符串中出现最长不重复的子串

/ 默认分类 / 没有评论 / 515浏览

基于go的实现

func lengthOfNonRepeatingSubStr(s string) int {
	lastCorrected := make(map[byte]int)
	start := 0
	maxLength := 0
	for i, ch := range []byte(s) {
		if lastI, ok := lastCorrected[ch]; ok && lastI >= start {
			start = lastCorrected[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastCorrected [ch] = i
	}
	return maxLength
}