Skip to content

Commit 2a97aa8

Browse files
committed
Add Longest Substring solution
1 parent 93db35a commit 2a97aa8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

longest_substring.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func lengthOfLongestSubstring(s string) int {
9+
longest := ""
10+
current := ""
11+
12+
for _, v := range s {
13+
vStr := string(v)
14+
15+
// Faster
16+
oldIndex := strings.Index(current, vStr)
17+
if oldIndex > -1 {
18+
fmt.Printf("\nFound %s at %d, Resetting %s to: %s", vStr, oldIndex, current, current[oldIndex + 1:])
19+
current = current[(oldIndex + 1):]
20+
}
21+
22+
current += string(vStr)
23+
24+
if len(current) > len(longest) {
25+
longest = current
26+
}
27+
}
28+
29+
fmt.Printf("\nLongest String: %s", longest)
30+
return len(longest)
31+
}
32+
33+
func main() {
34+
test1 := "pwwkew"
35+
test2 := "dvdf"
36+
fmt.Printf("\n%d", lengthOfLongestSubstring(test1))
37+
fmt.Printf("\n%d", lengthOfLongestSubstring(test2))
38+
}

0 commit comments

Comments
 (0)