Skip to content

Commit 8bccad8

Browse files
committed
Create: 0242-valid-anagram.scala
1 parent 80d28c8 commit 8bccad8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: scala/0242-valid-anagram.scala

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(s + t)
2+
// Space Comeplexity: O(s + t)
3+
4+
import scala.collection.mutable.Map
5+
6+
object Solution {
7+
def isAnagram(s: String, t: String): Boolean = {
8+
if (s.length() != t.length())
9+
return false
10+
11+
var charCount = Map[Char, Int]()
12+
13+
for (c <- s) {
14+
if (charCount.contains(c))
15+
charCount(c) = charCount(c) + 1
16+
else
17+
charCount += (c -> 1)
18+
}
19+
20+
for (c <- t) {
21+
if (charCount.contains(c))
22+
charCount(c) = charCount(c) - 1
23+
else
24+
charCount += (c -> 1)
25+
}
26+
27+
for ((_, v) <- charCount)
28+
if (v != 0)
29+
return false
30+
31+
return true
32+
}
33+
}

0 commit comments

Comments
 (0)