master
1package example
2
3import "testing"
4
5// Given a string, find the length of the longest substring without repeating characters.
6
7func lengthOfLongestSubstring(s string) int {
8 max, count, runes := 0, 0, []rune(s)
9
10 for i, item := range runes {
11 if i == 0 || item == runes[i-1] {
12 count = 0
13 }
14
15 count += 1
16 if count > max {
17 max = count
18 }
19 }
20
21 return max
22}
23
24func TestLongestSubstring(t *testing.T) {
25 t.Run("abrkaabcdefghijjxxx returns 10", func(t *testing.T) {
26 result := lengthOfLongestSubstring("abrkaabcdefghijjxxx")
27 if result != 10 {
28 t.Errorf("Expected: 10, Got: %v", result)
29 }
30 })
31}