Skip to content

Commit f8dbd68

Browse files
committed
242. Valid Anagram
1 parent 8478c42 commit f8dbd68

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-30%20%2F%203018-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-31%20%2F%203022-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
55
![Language: Rust](https://img.shields.io/badge/language-Rust-00ADD8?logo=rust)
66
![Language: Bash](https://img.shields.io/badge/language-Bash-00ADD8?logo=gnubash&logoColor=f5f5f5)
@@ -30,6 +30,7 @@
3030
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 | Stack |
3131
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Go](go/0217_contains_duplicate) | 🟢 | Array, Hash Table |
3232
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 | Tree, BFS, DFS, Binary Tree |
33+
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Go](go/0242_valid_anagram) | 🟢 | Hash Table, String |
3334
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Go](go/0268_missing_number) | 🟢 | Array, Math, Bit Manipulation |
3435
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 | Array, Pointer |
3536
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string), [Rust](rust/_0344_reverse_string) | 🟢 | String, Pointer |

go/0242_valid_anagram/solution.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package valid_anagram
2+
3+
func isAnagram(s string, t string) bool {
4+
if len(s) != len(t) {
5+
return false
6+
}
7+
8+
m := make(map[byte]int16, len(s))
9+
for i := 0; i < len(s); i++ {
10+
m[s[i]]++
11+
m[t[i]]--
12+
}
13+
14+
for _, val := range m {
15+
if val != 0 {
16+
return false
17+
}
18+
}
19+
20+
return true
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package valid_anagram
2+
3+
import "testing"
4+
5+
func Test_isAnagram(t *testing.T) {
6+
tests := []struct {
7+
inputS string
8+
inputT string
9+
want bool
10+
}{
11+
{inputS: "anagram", inputT: "nagaram", want: true},
12+
{inputS: "restful", inputT: "fluster", want: true},
13+
{inputS: "rat", inputT: "car", want: false},
14+
{inputS: "x", inputT: "x", want: true},
15+
{inputS: "a", inputT: "ab", want: false},
16+
{inputS: "aa", inputT: "bb", want: false},
17+
{inputS: "aac", inputT: "bcb", want: false},
18+
{inputS: "ab", inputT: "ba", want: true},
19+
}
20+
21+
for i, tc := range tests {
22+
got := isAnagram(tc.inputS, tc.inputT)
23+
if tc.want != got {
24+
t.Fatalf("Case #%d: expected: %v, got: %v", i+1, tc.want, got)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)