Skip to content

Commit 081f896

Browse files
committed
Create: 0049-group-anagrams.scala
1 parent 4c7749d commit 081f896

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: scala/0049-group-anagrams.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import scala.collection.mutable.{Map => MMap}
2+
object Solution {
3+
def groupAnagrams(strs: Array[String]): List[List[String]] = {
4+
val resultMap = MMap[MMap[Char, Int], List[String]]()
5+
strs
6+
.map(str => (str, letterCountMap(str)))
7+
.foreach { tupled =>
8+
tupled match {
9+
case (str, aMap) => {
10+
resultMap.updateWith(aMap) {
11+
case Some(listOfStrings) => Some(listOfStrings :+ str)
12+
case None => Some(List(str))
13+
}
14+
}
15+
}
16+
}
17+
resultMap.values.toList
18+
}
19+
20+
def letterCountMap(str: String): MMap[Char, Int] = {
21+
val map = MMap[Char, Int]()
22+
str.foreach{ char =>
23+
map.updateWith(char){
24+
case Some(value) => Some(value + 1)
25+
case None => Some(1)
26+
}
27+
}
28+
map
29+
}
30+
}

0 commit comments

Comments
 (0)