Commit 05fee73

mo khan <mo@mokhan.ca>
2022-04-11 18:52:58
solve in golang
1 parent 68a2dcc
Changed files (1)
2022
2022/04/11/main_test.go
@@ -0,0 +1,31 @@
+package example
+
+import "testing"
+
+// Given a string, find the length of the longest substring without repeating characters.
+
+func lengthOfLongestSubstring(s string) int {
+	max, count, runes := 0, 0, []rune(s)
+
+	for i, item := range runes {
+		if i == 0 || item == runes[i-1] {
+			count = 0
+		}
+
+		count += 1
+		if count > max {
+			max = count
+		}
+	}
+
+	return max
+}
+
+func TestLongestSubstring(t *testing.T) {
+	t.Run("abrkaabcdefghijjxxx returns 10", func(t *testing.T) {
+		result := lengthOfLongestSubstring("abrkaabcdefghijjxxx")
+		if result != 10 {
+			t.Errorf("Expected: 10, Got: %v", result)
+		}
+	})
+}