package main

import "fmt"

// 优化
var lastOccurrend = make([]int, 0xffff) // 65535
func lengthOfNonRepeatingSubStr(s string) int {
	//lastOccurrend := make(map[rune]int)

	for i := range lastOccurrend {
		lastOccurrend[i] = -1
	}

	start := 0
	maxLength := 0
	// for i ,ch := range[]byte(s){   byte 修改rune 支持中文件
	for i, ch := range []rune(s) {
		if lastI := lastOccurrend[ch]; lastI != -1 && lastI >= start {
			start = lastOccurrend[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurrend[ch] = i

	}
	//for v, _ := range lastOccurrend {
	//	fmt.Printf("(%c )", v)
	//}
	//fmt.Println()

	return maxLength
}

func main() {
	fmt.Println(lengthOfNonRepeatingSubStr("dafwefwefwfsdfsdf"))
	fmt.Println(lengthOfNonRepeatingSubStr("我家你中国，我在家乡"))
}
