Skip to content

Commit 4228ce2

Browse files
committed
Minor modifications
1 parent d5b6e8d commit 4228ce2

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

strings/lengthOfLongestSubstring.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import ("fmt"
99

1010
func lengthOfLongestSubstring(s string) int {
1111
charHash := make(map[string]int)
12-
start := -1
12+
start := 0
1313
maxLen := 0
1414
for idx,rune := range s {
1515
if valIdx,ok := charHash[string(rune)] ; ok {
16-
start = valIdx
16+
for _,rune := range s[start:valIdx] {
17+
delete(charHash,string(rune))
18+
}
19+
start = valIdx + 1
1720
charHash[string(rune)] = idx
1821
} else {
1922
charHash[string(rune)] = idx
20-
if maxLen < (idx - start) {
21-
maxLen = idx - start
23+
if maxLen < (idx - start + 1) {
24+
maxLen = idx - start + 1
2225
}
2326
}
2427
}
@@ -28,6 +31,7 @@ func lengthOfLongestSubstring(s string) int {
2831
func main() {
2932
//s1 := "abcabcbb"
3033
s2 := "abcdecdfghi"
34+
//s2 := "tmmzuxt"
3135
length := lengthOfLongestSubstring(s2)
3236
fmt.Println("Length of Longest Substring in the string ",s2,"is :",length)
3337
}

0 commit comments

Comments
 (0)