Skip to content

Commit 8cdf024

Browse files
committed
Algorithm to find the length of longest substring
1 parent 12dc464 commit 8cdf024

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

strings/lengthOfLongestSubstring.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Length of Longest Substring
3+
*/
4+
5+
package main
6+
7+
import ("fmt"
8+
)
9+
10+
func lengthOfLongestSubstring(s string) int {
11+
charHash := make(map[string]int)
12+
start := -1
13+
maxLen := 0
14+
for idx,rune := range s {
15+
if valIdx,ok := charHash[string(rune)] ; ok {
16+
start = valIdx
17+
charHash[string(rune)] = idx
18+
} else {
19+
charHash[string(rune)] = idx
20+
if maxLen < (idx - start) {
21+
maxLen = idx - start
22+
}
23+
}
24+
}
25+
return maxLen
26+
}
27+
28+
func main() {
29+
//s1 := "abcabcbb"
30+
s2 := "abcdecdfghi"
31+
length := lengthOfLongestSubstring(s2)
32+
fmt.Println("Length of Longest Substring in the string ",s2,"is :",length)
33+
}

0 commit comments

Comments
 (0)