-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path819-MostCommonWord.go
More file actions
79 lines (71 loc) · 2.98 KB
/
819-MostCommonWord.go
File metadata and controls
79 lines (71 loc) · 2.98 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
// 819. Most Common Word
// Given a string paragraph and a string array of the banned words banned,
// return the most frequent word that is not banned.
// It is guaranteed there is at least one word that is not banned, and that the answer is unique.
// The words in paragraph are case-insensitive and the answer should be returned in lowercase.
// Example 1:
// Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
// Output: "ball"
// Explanation:
// "hit" occurs 3 times, but it is a banned word.
// "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
// Note that words in the paragraph are not case sensitive,
// that punctuation is ignored (even if adjacent to words, such as "ball,"),
// and that "hit" isn't the answer even though it occurs more because it is banned.
// Example 2:
// Input: paragraph = "a.", banned = []
// Output: "a"
// Constraints:
// 1 <= paragraph.length <= 1000
// paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
// 0 <= banned.length <= 100
// 1 <= banned[i].length <= 10
// banned[i] consists of only lowercase English letters.
import "fmt"
import "strings"
func mostCommonWord(paragraph string, banned []string) string {
ban, dic := make(map[string]bool), make(map[string]int)
word, freq := "", 0
for _, v := range banned {
ban[v] = true
}
para, check := strings.ToLower(paragraph) + " ", "" // 转成 小写
for _, c := range para {
a := int(c)
if a > 96 && a < 123 { // 取出连续的字符
check += string(c)
} else {
if check != "" {
if ban[check] { // 如果是ban的字符直接处理下个
check = ""
continue
} else {
dic[check]++ // 累加出现次数
if dic[check] > freq { // 出现最多的字符串
freq = dic[check]
word = check
}
check = ""
}
}
}
}
return word
}
func main() {
// Example 1:
// Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
// Output: "ball"
// Explanation:
// "hit" occurs 3 times, but it is a banned word.
// "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
// Note that words in the paragraph are not case sensitive,
// that punctuation is ignored (even if adjacent to words, such as "ball,"),
// and that "hit" isn't the answer even though it occurs more because it is banned.
fmt.Println(mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", []string{"hit"})) // "ball"
// Example 2:
// Input: paragraph = "a.", banned = []
// Output: "a"
fmt.Println(mostCommonWord("a.", []string{})) // "a"
}