|
@@ -0,0 +1,62 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import "fmt"
|
|
|
+
|
|
|
+func main() {
|
|
|
+ var s string
|
|
|
+
|
|
|
+ s = ""
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "a"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "aa"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "ab"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "abca"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "ababc"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "abcabcda"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "abcabcbb"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "bbbbb"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "pwwkew"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+
|
|
|
+ s = "dvdf"
|
|
|
+ fmt.Println(lengthOfLongestSubstring(s))
|
|
|
+}
|
|
|
+
|
|
|
+func lengthOfLongestSubstring(s string) int {
|
|
|
+ var cursor, maxCount int = 0, 0
|
|
|
+ var record = map[byte]int{}
|
|
|
+ for i := 0; i < len(s); i++ {
|
|
|
+ if v, ok := record[s[i]]; !ok || (ok && v < cursor) {
|
|
|
+ if i == len(s)-1 && i-cursor+1 > maxCount {
|
|
|
+ maxCount = i - cursor + 1
|
|
|
+ }
|
|
|
+ record[s[i]] = i
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ if i-cursor > maxCount {
|
|
|
+ maxCount = i - cursor
|
|
|
+ }
|
|
|
+ cursor++
|
|
|
+ record[s[i]] = i
|
|
|
+
|
|
|
+ }
|
|
|
+ return maxCount
|
|
|
+}
|