Skip to content

Commit

Permalink
Create: 0242-valid-anagram.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSWE committed Jan 6, 2023
1 parent 80d28c8 commit 8bccad8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions scala/0242-valid-anagram.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity: O(s + t)
// Space Comeplexity: O(s + t)

import scala.collection.mutable.Map

object Solution {
def isAnagram(s: String, t: String): Boolean = {
if (s.length() != t.length())
return false

var charCount = Map[Char, Int]()

for (c <- s) {
if (charCount.contains(c))
charCount(c) = charCount(c) + 1
else
charCount += (c -> 1)
}

for (c <- t) {
if (charCount.contains(c))
charCount(c) = charCount(c) - 1
else
charCount += (c -> 1)
}

for ((_, v) <- charCount)
if (v != 0)
return false

return true
}
}

0 comments on commit 8bccad8

Please sign in to comment.