Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2393 from krishna1m/0049-group-anagrams
Browse files Browse the repository at this point in the history
Create: 0049-group-anagrams.scala
  • Loading branch information
krishna1m authored Jul 6, 2023
2 parents 1695ec8 + 081f896 commit 0a271ef
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions scala/0049-group-anagrams.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import scala.collection.mutable.{Map => MMap}
object Solution {
def groupAnagrams(strs: Array[String]): List[List[String]] = {
val resultMap = MMap[MMap[Char, Int], List[String]]()
strs
.map(str => (str, letterCountMap(str)))
.foreach { tupled =>
tupled match {
case (str, aMap) => {
resultMap.updateWith(aMap) {
case Some(listOfStrings) => Some(listOfStrings :+ str)
case None => Some(List(str))
}
}
}
}
resultMap.values.toList
}

def letterCountMap(str: String): MMap[Char, Int] = {
val map = MMap[Char, Int]()
str.foreach{ char =>
map.updateWith(char){
case Some(value) => Some(value + 1)
case None => Some(1)
}
}
map
}
}

0 comments on commit 0a271ef

Please sign in to comment.