Skip to content

Commit 6e98ee9

Browse files
committed
Add valid anagram solution
1 parent 6a0a279 commit 6e98ee9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

valid-anagram/hyunjung-choi.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
fun isAnagram(s: String, t: String): Boolean {
3+
if (s.length != t.length) return false
4+
5+
val charCount = mutableMapOf<Char, Int>()
6+
7+
s.forEach { ch ->
8+
charCount.put(ch, charCount.getOrDefault(ch, 0) + 1)
9+
}
10+
11+
t.forEach { ch ->
12+
val count = charCount.getOrDefault(ch, 0)
13+
if (count == 0) return false
14+
charCount[ch] = count - 1
15+
}
16+
17+
return true
18+
}
19+
}

0 commit comments

Comments
 (0)