forked from dylansun/leetcode-cn-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo819.scala
37 lines (33 loc) · 849 Bytes
/
No819.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Created by lilisun on 3/9/19.
*/
object No819 {
def mostCommonWord(paragraph: String, banned: Array[String]): String = {
paragraph
.toLowerCase()
.replaceAll("[!?',;.]", " ")
.split(" ")
.filter(_ != "")
.filter(x => !banned.contains(x))
.groupBy( x => x)
.map(x => (x._1, x._2.length))
.toList
.sortBy(x => (-x._2 , x._1))
.head._1
}
def main(args: Array[String]): Unit = {
val s = "Bob hit a ball, the hit BALL flew far after it was hit."
val banned = Array("hit")
println(s.replaceAll("[!?',;.]", " ")
.toLowerCase()
.split(" ")
.filter(_ != "")
.filter(x => !banned.contains(x))
.groupBy(x => x)
.map(x => (x._1, x._2.length))
.toList
.sortBy(x => (-x._2 , x._1))
.head._1
)
}
}