Skip to content

Commit

Permalink
Merge pull request #71 from arunsathiya/solve/3-longest-substring-wit…
Browse files Browse the repository at this point in the history
…hout-repeating-characters

3. Longest Substring Without Repeating Characters
  • Loading branch information
arunsathiya committed Feb 24, 2024
2 parents d6a3a0b + 70a8e3e commit abb185f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/3-longest-substring-without-repeating-characters/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

func lengthOfLongestSubstring(s string) int {
start := 0
maxLength := 0
charhm := make(map[rune]int)
for end, char := range s {
if trackedIdx, found := charhm[char]; found {
start = trackedIdx + 1
}
charhm[char] = end
currentLength := end - start + 1
if currentLength > maxLength {
maxLength = currentLength
}
}
return maxLength
}
24 changes: 24 additions & 0 deletions src/3-longest-substring-without-repeating-characters/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "testing"

func TestLengthOfLongestSubstring(t *testing.T) {
testCases := []struct {
name string
input string
expected int
}{
{"Example 1", "abcabcbb", 3},
{"Example 2", "bbbbb", 1},
{"Example 3", "pwwkew", 3},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := lengthOfLongestSubstring(tc.input)
if actual != tc.expected {
t.Errorf("For input '%s', expected length %d but got %d", tc.input, tc.expected, actual)
}
})
}
}

0 comments on commit abb185f

Please sign in to comment.