We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 1695ec8 + 081f896 commit 0a271efCopy full SHA for 0a271ef
scala/0049-group-anagrams.scala
@@ -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