Skip to content

Commit 8437115

Browse files
authored
[Arrays & Hashing][Valid Anagram] - Implement solution for Leetcode 242 #7
1 parent d462e57 commit 8437115

File tree

6 files changed

+114
-41
lines changed

6 files changed

+114
-41
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ jobs:
2828
- name: Upload coverage to Codecov
2929
uses: codecov/codecov-action@v5
3030
with:
31-
token: ${{ secrets.CODECOV_TOKEN }}
31+
token: ${{ secrets.CODECOV_TOKEN }}
32+
files: coverage.txt
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package validanagram
2+
3+
import (
4+
"sort"
5+
"strings"
6+
)
7+
8+
// Time complexity: O(n log n)
9+
// Space complexity : O(n)
10+
func IsAnagramSort(s, t string) bool {
11+
if len(s) != len(t) {
12+
return false
13+
}
14+
15+
sArr := strings.Split(s, "")
16+
tArr := strings.Split(t, "")
17+
18+
sort.Strings(sArr)
19+
sort.Strings(tArr)
20+
21+
return strings.Join(sArr, "") == strings.Join(tArr, "")
22+
}
23+
24+
// Time complexity: O(n)
25+
// Space complexity: O(n)
26+
func IsAnagramMap(s, t string) bool {
27+
if len(s) != len(t) {
28+
return false
29+
}
30+
31+
counts := make(map[rune]int)
32+
for _, r := range s {
33+
counts[r]++
34+
}
35+
for _, r := range t {
36+
counts[r]--
37+
if counts[r] < 0 {
38+
return false
39+
}
40+
}
41+
return true
42+
}
43+
44+
// Time complexity: O(n)
45+
// Space complexity: O(1)
46+
func IsAnagramArray(s, t string) bool {
47+
if len(s) != len(t) {
48+
return false
49+
}
50+
51+
var counts [26]int
52+
53+
for i := 0; i < len(s); i++ {
54+
// we ensure the character is between 'a' and 'z'
55+
if s[i] < 'a' || s[i] > 'z' || t[i] < 'a' || t[i] > 'z' {
56+
return false
57+
}
58+
59+
counts[s[i]-'a']++
60+
counts[t[i]-'a']--
61+
}
62+
63+
for _, v := range counts {
64+
if v != 0 {
65+
return false
66+
}
67+
}
68+
return true
69+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package validanagram
2+
3+
import "testing"
4+
5+
func TestIsAnagram(t *testing.T) {
6+
cases := []struct {
7+
name string
8+
fn func(string, string) bool
9+
s, t string
10+
expected bool
11+
}{
12+
// IsAnagramSort
13+
{"Sort - Anagram", IsAnagramSort, "listen", "silent", true},
14+
{"Sort - Not Anagram", IsAnagramSort, "hello", "world", false},
15+
{"Sort - Empty Strings", IsAnagramSort, "", "", true},
16+
{"Sort - Different Lengths", IsAnagramSort, "a", "ab", false},
17+
{"Sort - Unicode Anagrams", IsAnagramSort, "école", "éocle", true},
18+
19+
// IsAnagramMap
20+
{"Map - Anagram", IsAnagramMap, "listen", "silent", true},
21+
{"Map - Not Anagram", IsAnagramMap, "hello", "world", false},
22+
{"Map - Empty Strings", IsAnagramMap, "", "", true},
23+
{"Map - Different Lengths", IsAnagramMap, "a", "ab", false},
24+
{"Map - Unicode Anagrams", IsAnagramMap, "école", "éocle", true},
25+
26+
// IsAnagramArray
27+
{"Array - Anagram", IsAnagramArray, "listen", "silent", true},
28+
{"Array - Not Anagram", IsAnagramArray, "hello", "world", false},
29+
{"Array - Empty Strings", IsAnagramArray, "", "", true},
30+
{"Array - Different Lengths", IsAnagramArray, "a", "ab", false},
31+
{"Array - Unicode Anagrams", IsAnagramArray, "école", "éocle", false},
32+
}
33+
34+
for _, c := range cases {
35+
t.Run(c.name, func(t *testing.T) {
36+
got := c.fn(c.s, c.t)
37+
if got != c.expected {
38+
t.Errorf("%s failed: expected %v, got %v", c.name, c.expected, got)
39+
40+
}
41+
})
42+
}
43+
}

coverage.out

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1 @@
11
mode: set
2-
github.com/javy99/problem-solving-golang/main/hello.go:3.21,5.2 1 1
3-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:7.52,8.33 1 1
4-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:8.33,9.38 1 1
5-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:9.38,10.26 1 1
6-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:10.26,12.5 1 1
7-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:15.2,15.14 1 1
8-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:20.48,22.33 2 1
9-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:22.33,23.27 1 1
10-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:23.27,25.4 1 1
11-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:27.2,27.14 1 1
12-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:32.49,34.27 2 1
13-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:34.27,35.16 1 1
14-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:35.16,37.4 1 1
15-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:38.3,38.19 1 1
16-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:40.2,40.14 1 1
17-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:45.51,47.27 2 1
18-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:47.27,48.29 1 1
19-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:48.29,50.4 1 1
20-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:51.3,51.25 1 1
21-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:53.2,53.14 1 1
22-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:58.50,60.27 2 1
23-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:60.27,62.22 2 1
24-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:62.22,64.4 1 1
25-
github.com/javy99/problem-solving-golang/1_arrays_and_hashing/1_contains_duplicate/solution.go:66.2,66.14 1 1

main/hello.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

main/hello_test.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)