Skip to content

Commit 17d5ca7

Browse files
committed
feat: add solutions to lc problem: No.0819
No.0819.Most Common Word
1 parent 330a360 commit 17d5ca7

File tree

6 files changed

+253
-54
lines changed

6 files changed

+253
-54
lines changed

solution/0800-0899/0819.Most Common Word/README.md

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ banned = ["hit"]
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
“正则 + 计数器”实现。
48+
**方法一:正则匹配/双指针 + 哈希表**
49+
50+
正则匹配(或双指针)找出所有单词,用哈希表统计每个单词出现的频率,找到出现未在 banned 中出现且频率最大的单词。
4951

5052
<!-- tabs:start -->
5153

@@ -56,11 +58,9 @@ banned = [&quot;hit&quot;]
5658
```python
5759
class Solution:
5860
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
59-
paragraph = Counter(re.findall('[a-z]+', paragraph.lower()))
60-
banned_words = set(banned)
61-
for word, _ in paragraph.most_common():
62-
if word not in banned_words:
63-
return word
61+
s = set(banned)
62+
p = Counter(re.findall('[a-z]+', paragraph.lower()))
63+
return next(word for word, _ in p.most_common() if word not in s)
6464
```
6565

6666
### **Java**
@@ -72,13 +72,15 @@ import java.util.regex.Matcher;
7272
import java.util.regex.Pattern;
7373

7474
class Solution {
75+
private static Pattern pattern = Pattern.compile("[a-z]+");
76+
7577
public String mostCommonWord(String paragraph, String[] banned) {
7678
Set<String> bannedWords = new HashSet<>();
7779
for (String word : banned) {
7880
bannedWords.add(word);
7981
}
8082
Map<String, Integer> counter = new HashMap<>();
81-
Matcher matcher = Pattern.compile("[a-z]+").matcher(paragraph.toLowerCase());
83+
Matcher matcher = pattern.matcher(paragraph.toLowerCase());
8284
while (matcher.find()) {
8385
String word = matcher.group();
8486
if (bannedWords.contains(word)) {
@@ -87,14 +89,14 @@ class Solution {
8789
counter.put(word, counter.getOrDefault(word, 0) + 1);
8890
}
8991
int max = Integer.MIN_VALUE;
90-
String res = null;
92+
String ans = null;
9193
for (Map.Entry<String, Integer> entry : counter.entrySet()) {
9294
if (entry.getValue() > max) {
9395
max = entry.getValue();
94-
res = entry.getKey();
96+
ans = entry.getKey();
9597
}
9698
}
97-
return res;
99+
return ans;
98100
}
99101
}
100102
```
@@ -144,6 +146,75 @@ impl Solution {
144146
}
145147
```
146148

149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
string mostCommonWord(string paragraph, vector<string>& banned) {
155+
unordered_set<string> s(banned.begin(), banned.end());
156+
unordered_map<string, int> counter;
157+
string ans;
158+
for (int i = 0, mx = 0, n = paragraph.size(); i < n;)
159+
{
160+
if (!isalpha(paragraph[i]) && (++i > 0)) continue;
161+
int j = i;
162+
string word;
163+
while (j < n && isalpha(paragraph[j]))
164+
{
165+
word.push_back(tolower(paragraph[j]));
166+
++j;
167+
}
168+
i = j + 1;
169+
if (s.count(word)) continue;
170+
++counter[word];
171+
if (counter[word] > mx)
172+
{
173+
ans = word;
174+
mx = counter[word];
175+
}
176+
}
177+
return ans;
178+
}
179+
};
180+
```
181+
182+
### **Go**
183+
184+
```go
185+
func mostCommonWord(paragraph string, banned []string) string {
186+
s := make(map[string]bool)
187+
for _, w := range banned {
188+
s[w] = true
189+
}
190+
counter := make(map[string]int)
191+
var ans string
192+
for i, mx, n := 0, 0, len(paragraph); i < n; {
193+
if !unicode.IsLetter(rune(paragraph[i])) {
194+
i++
195+
continue
196+
}
197+
j := i
198+
var word []byte
199+
for j < n && unicode.IsLetter(rune(paragraph[j])) {
200+
word = append(word, byte(unicode.ToLower(rune(paragraph[j]))))
201+
j++
202+
}
203+
i = j + 1
204+
t := string(word)
205+
if s[t] {
206+
continue
207+
}
208+
counter[t]++
209+
if counter[t] > mx {
210+
ans = t
211+
mx = counter[t]
212+
}
213+
}
214+
return ans
215+
}
216+
```
217+
147218
### **...**
148219

149220
```

solution/0800-0899/0819.Most Common Word/README_EN.md

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ and that &quot;hit&quot; isn&#39;t the answer even though it occurs more because
4949
```python
5050
class Solution:
5151
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
52-
paragraph = Counter(re.findall('[a-z]+', paragraph.lower()))
53-
banned_words = set(banned)
54-
for word, _ in paragraph.most_common():
55-
if word not in banned_words:
56-
return word
52+
s = set(banned)
53+
p = Counter(re.findall('[a-z]+', paragraph.lower()))
54+
return next(word for word, _ in p.most_common() if word not in s)
5755
```
5856

5957
### **Java**
@@ -63,13 +61,15 @@ import java.util.regex.Matcher;
6361
import java.util.regex.Pattern;
6462

6563
class Solution {
64+
private static Pattern pattern = Pattern.compile("[a-z]+");
65+
6666
public String mostCommonWord(String paragraph, String[] banned) {
6767
Set<String> bannedWords = new HashSet<>();
6868
for (String word : banned) {
6969
bannedWords.add(word);
7070
}
7171
Map<String, Integer> counter = new HashMap<>();
72-
Matcher matcher = Pattern.compile("[a-z]+").matcher(paragraph.toLowerCase());
72+
Matcher matcher = pattern.matcher(paragraph.toLowerCase());
7373
while (matcher.find()) {
7474
String word = matcher.group();
7575
if (bannedWords.contains(word)) {
@@ -78,15 +78,84 @@ class Solution {
7878
counter.put(word, counter.getOrDefault(word, 0) + 1);
7979
}
8080
int max = Integer.MIN_VALUE;
81-
String res = null;
81+
String ans = null;
8282
for (Map.Entry<String, Integer> entry : counter.entrySet()) {
8383
if (entry.getValue() > max) {
8484
max = entry.getValue();
85-
res = entry.getKey();
85+
ans = entry.getKey();
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
string mostCommonWord(string paragraph, vector<string>& banned) {
99+
unordered_set<string> s(banned.begin(), banned.end());
100+
unordered_map<string, int> counter;
101+
string ans;
102+
for (int i = 0, mx = 0, n = paragraph.size(); i < n;)
103+
{
104+
if (!isalpha(paragraph[i]) && (++i > 0)) continue;
105+
int j = i;
106+
string word;
107+
while (j < n && isalpha(paragraph[j]))
108+
{
109+
word.push_back(tolower(paragraph[j]));
110+
++j;
111+
}
112+
i = j + 1;
113+
if (s.count(word)) continue;
114+
++counter[word];
115+
if (counter[word] > mx)
116+
{
117+
ans = word;
118+
mx = counter[word];
86119
}
87120
}
88-
return res;
121+
return ans;
89122
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func mostCommonWord(paragraph string, banned []string) string {
130+
s := make(map[string]bool)
131+
for _, w := range banned {
132+
s[w] = true
133+
}
134+
counter := make(map[string]int)
135+
var ans string
136+
for i, mx, n := 0, 0, len(paragraph); i < n; {
137+
if !unicode.IsLetter(rune(paragraph[i])) {
138+
i++
139+
continue
140+
}
141+
j := i
142+
var word []byte
143+
for j < n && unicode.IsLetter(rune(paragraph[j])) {
144+
word = append(word, byte(unicode.ToLower(rune(paragraph[j]))))
145+
j++
146+
}
147+
i = j + 1
148+
t := string(word)
149+
if s[t] {
150+
continue
151+
}
152+
counter[t]++
153+
if counter[t] > mx {
154+
ans = t
155+
mx = counter[t]
156+
}
157+
}
158+
return ans
90159
}
91160
```
92161

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string mostCommonWord(string paragraph, vector<string>& banned) {
4+
unordered_set<string> s(banned.begin(), banned.end());
5+
unordered_map<string, int> counter;
6+
string ans;
7+
for (int i = 0, mx = 0, n = paragraph.size(); i < n;)
8+
{
9+
if (!isalpha(paragraph[i]) && (++i > 0)) continue;
10+
int j = i;
11+
string word;
12+
while (j < n && isalpha(paragraph[j]))
13+
{
14+
word.push_back(tolower(paragraph[j]));
15+
++j;
16+
}
17+
i = j + 1;
18+
if (s.count(word)) continue;
19+
++counter[word];
20+
if (counter[word] > mx)
21+
{
22+
ans = word;
23+
mx = counter[word];
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func mostCommonWord(paragraph string, banned []string) string {
2+
s := make(map[string]bool)
3+
for _, w := range banned {
4+
s[w] = true
5+
}
6+
counter := make(map[string]int)
7+
var ans string
8+
for i, mx, n := 0, 0, len(paragraph); i < n; {
9+
if !unicode.IsLetter(rune(paragraph[i])) {
10+
i++
11+
continue
12+
}
13+
j := i
14+
var word []byte
15+
for j < n && unicode.IsLetter(rune(paragraph[j])) {
16+
word = append(word, byte(unicode.ToLower(rune(paragraph[j]))))
17+
j++
18+
}
19+
i = j + 1
20+
t := string(word)
21+
if s[t] {
22+
continue
23+
}
24+
counter[t]++
25+
if counter[t] > mx {
26+
ans = t
27+
mx = counter[t]
28+
}
29+
}
30+
return ans
31+
}
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import java.util.regex.Matcher;
2-
import java.util.regex.Pattern;
3-
4-
class Solution {
5-
public String mostCommonWord(String paragraph, String[] banned) {
6-
Set<String> bannedWords = new HashSet<>();
7-
for (String word : banned) {
8-
bannedWords.add(word);
9-
}
10-
Map<String, Integer> counter = new HashMap<>();
11-
Matcher matcher = Pattern.compile("[a-z]+").matcher(paragraph.toLowerCase());
12-
while (matcher.find()) {
13-
String word = matcher.group();
14-
if (bannedWords.contains(word)) {
15-
continue;
16-
}
17-
counter.put(word, counter.getOrDefault(word, 0) + 1);
18-
}
19-
int max = Integer.MIN_VALUE;
20-
String res = null;
21-
for (Map.Entry<String, Integer> entry : counter.entrySet()) {
22-
if (entry.getValue() > max) {
23-
max = entry.getValue();
24-
res = entry.getKey();
25-
}
26-
}
27-
return res;
28-
}
1+
import java.util.regex.Matcher;
2+
import java.util.regex.Pattern;
3+
4+
class Solution {
5+
private static Pattern pattern = Pattern.compile("[a-z]+");
6+
7+
public String mostCommonWord(String paragraph, String[] banned) {
8+
Set<String> bannedWords = new HashSet<>();
9+
for (String word : banned) {
10+
bannedWords.add(word);
11+
}
12+
Map<String, Integer> counter = new HashMap<>();
13+
Matcher matcher = pattern.matcher(paragraph.toLowerCase());
14+
while (matcher.find()) {
15+
String word = matcher.group();
16+
if (bannedWords.contains(word)) {
17+
continue;
18+
}
19+
counter.put(word, counter.getOrDefault(word, 0) + 1);
20+
}
21+
int max = Integer.MIN_VALUE;
22+
String ans = null;
23+
for (Map.Entry<String, Integer> entry : counter.entrySet()) {
24+
if (entry.getValue() > max) {
25+
max = entry.getValue();
26+
ans = entry.getKey();
27+
}
28+
}
29+
return ans;
30+
}
2931
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
class Solution:
2-
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
3-
paragraph = Counter(re.findall('[a-z]+', paragraph.lower()))
4-
banned_words = set(banned)
5-
for word, _ in paragraph.most_common():
6-
if word not in banned_words:
7-
return word
1+
class Solution:
2+
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
3+
s = set(banned)
4+
p = Counter(re.findall('[a-z]+', paragraph.lower()))
5+
return next(word for word, _ in p.most_common() if word not in s)

0 commit comments

Comments
 (0)