3.无重复字符的最长子串

51 阅读1分钟

题目:
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
算法: 字符串的术语 子序列:字符串首尾中间挖掉一些字符得到。
子串(连续子序列):字符串首尾截断一部分字符得到。 字符:字符串顺序可以打乱

func lengthOfLongestSubstring(s string) int {
	charLastApperedIndex := make(map[byte]int)
	max := 0
	start := 0
	for i := range s {
		if lasteIndex, ok := charLastApperedIndex[s[i]]; ok {
			if lasteIndex + 1 > start {
				start =  lasteIndex + 1
			}
			
		} 
		// fmt.Println(i, start, max)
		if i - start + 1 > max {
			max = i - start + 1
		}
		charLastApperedIndex[s[i]] = i
	}
	return max
}