From e9db700495a44c6230a78a41f13611e0ad880723 Mon Sep 17 00:00:00 2001 From: duruyao Date: Tue, 21 Mar 2023 16:10:55 +0800 Subject: [PATCH] Update longest-substring-without-repeating-characters.go --- ...-substring-without-repeating-characters.go | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/Golang/longest-substring-without-repeating-characters.go b/Golang/longest-substring-without-repeating-characters.go index 11dd57570c..08f70fed68 100644 --- a/Golang/longest-substring-without-repeating-characters.go +++ b/Golang/longest-substring-without-repeating-characters.go @@ -9,26 +9,17 @@ package leetcode // Note that the answer must be a substring, "pwke" is a subsequence and not a substring. // func lengthOfLongestSubstring(s string) int { - hashmap := map[byte]int{} - max := 0 - for i := range s { - _, ok := hashmap[s[i]] - if !ok { - hashmap[s[i]] = i - if len(hashmap) > max { - max = len(hashmap) - } - } else { - // remove repeated - oldI := hashmap[s[i]] - hashmap[s[i]] = i - - for key, value := range hashmap { - if value < oldI { - delete(hashmap, key) - } - } + maxLen := 0 + m := map[byte]int{} + for i, j := 0, 0; j < len(s); j++ { + idx, ok := m[s[j]] + if ok && idx >= i { + i = idx + 1 + } + if maxLen < j-i+1 { + maxLen = j - i + 1 } + m[s[j]] = j } - return max + return maxLen }