-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 867 Bytes
/
main.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", []string{"hit"}))
}
func mostCommonWord(p string, banned []string) string {
p = strings.ToLower(p)
mp := make(map[string]bool)
for _, v := range banned {
mp[v] = true
}
p = strings.Replace(p, ".", " ", -1)
p = strings.Replace(p, ",", " ", -1)
p = strings.Replace(p, "?", " ", -1)
p = strings.Replace(p, ";", " ", -1)
p = strings.Replace(p, "'", " ", -1)
p = strings.Replace(p, "!", " ", -1)
count := make(map[string]int)
arr := strings.Split(p, " ")
for _, v := range arr {
if v == "" {
continue
}
count[v]++
}
for k := range mp {
delete(count, k)
}
var (
result string
rCount int
)
for k, v := range count {
if v > rCount {
rCount = v
result = k
}
}
return result
}