Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit081f896

Browse files
committed
Create: 0049-group-anagrams.scala
1 parent4c7749d commit081f896

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎scala/0049-group-anagrams.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
importscala.collection.mutable.{Map=>MMap}
2+
objectSolution {
3+
defgroupAnagrams(strs:Array[String]):List[List[String]]= {
4+
valresultMap=MMap[MMap[Char,Int],List[String]]()
5+
strs
6+
.map(str=> (str, letterCountMap(str)))
7+
.foreach { tupled=>
8+
tupledmatch {
9+
case (str, aMap)=> {
10+
resultMap.updateWith(aMap) {
11+
caseSome(listOfStrings)=>Some(listOfStrings:+ str)
12+
caseNone=>Some(List(str))
13+
}
14+
}
15+
}
16+
}
17+
resultMap.values.toList
18+
}
19+
20+
defletterCountMap(str:String):MMap[Char,Int]= {
21+
valmap=MMap[Char,Int]()
22+
str.foreach{ char=>
23+
map.updateWith(char){
24+
caseSome(value)=>Some(value+1)
25+
caseNone=>Some(1)
26+
}
27+
}
28+
map
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp