Go 字符串

go查找字符串不重复最长子串

package main

import "fmt"

func lenOfNorepeatingSubStr(str string) int {
    lastChIndex := make(map[rune] int)
    startIndex := 0
    maxLeng := 0
    for index, ch := range []rune(str) {
        ci, ok := lastChIndex[ch]
        if ok && ci >= startIndex {
            startIndex = lastChIndex[ch] + 1
        }
        if index - startIndex + 1 > maxLeng {
            maxLeng = index - startIndex + 1
        }
        lastChIndex[ch] = index
    }
    return maxLeng
}

func main() {
    fmt.Println(lenOfNorepeatingSubStr("pwwkew"))
    fmt.Println(lenOfNorepeatingSubStr("一二而已"))
    fmt.Println(lenOfNorepeatingSubStr("一二二已"))
}

猜你喜欢

转载自blog.csdn.net/YeYuLuoJin/article/details/81391702