Skip to content

Commit 330a360

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

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,51 @@ class Solution {
9999
}
100100
```
101101

102+
### **TypeScript**
103+
104+
```ts
105+
function mostCommonWord(paragraph: string, banned: string[]): string {
106+
const s = paragraph.toLocaleLowerCase();
107+
const map = new Map<string, number>();
108+
const set = new Set<string>(banned);
109+
for (const word of s.split(/[^A-z]/)) {
110+
if (word === '' || set.has(word)) {
111+
continue;
112+
}
113+
map.set(word, (map.get(word) ?? 0) + 1);
114+
}
115+
return [...map.entries()].reduce(
116+
(r, v) => (v[1] > r[1] ? v : r),
117+
['', 0],
118+
)[0];
119+
}
120+
```
121+
122+
### **Rust**
123+
124+
```rust
125+
use std::collections::{HashMap, HashSet};
126+
impl Solution {
127+
pub fn most_common_word(mut paragraph: String, banned: Vec<String>) -> String {
128+
paragraph.make_ascii_lowercase();
129+
let banned: HashSet<&str> = banned.iter().map(String::as_str).collect();
130+
let mut map = HashMap::new();
131+
for word in paragraph.split(|c| !matches!(c, 'a'..='z')) {
132+
if word.is_empty() || banned.contains(word) {
133+
continue;
134+
}
135+
let val = map.get(&word).unwrap_or(&0) + 1;
136+
map.insert(word, val);
137+
}
138+
map.into_iter()
139+
.max_by_key(|&(_, v)| v)
140+
.unwrap()
141+
.0
142+
.to_string()
143+
}
144+
}
145+
```
146+
102147
### **...**
103148

104149
```

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,51 @@ class Solution {
9090
}
9191
```
9292

93+
### **TypeScript**
94+
95+
```ts
96+
function mostCommonWord(paragraph: string, banned: string[]): string {
97+
const s = paragraph.toLocaleLowerCase();
98+
const map = new Map<string, number>();
99+
const set = new Set<string>(banned);
100+
for (const word of s.split(/[^A-z]/)) {
101+
if (word === '' || set.has(word)) {
102+
continue;
103+
}
104+
map.set(word, (map.get(word) ?? 0) + 1);
105+
}
106+
return [...map.entries()].reduce(
107+
(r, v) => (v[1] > r[1] ? v : r),
108+
['', 0],
109+
)[0];
110+
}
111+
```
112+
113+
### **Rust**
114+
115+
```rust
116+
use std::collections::{HashMap, HashSet};
117+
impl Solution {
118+
pub fn most_common_word(mut paragraph: String, banned: Vec<String>) -> String {
119+
paragraph.make_ascii_lowercase();
120+
let banned: HashSet<&str> = banned.iter().map(String::as_str).collect();
121+
let mut map = HashMap::new();
122+
for word in paragraph.split(|c| !matches!(c, 'a'..='z')) {
123+
if word.is_empty() || banned.contains(word) {
124+
continue;
125+
}
126+
let val = map.get(&word).unwrap_or(&0) + 1;
127+
map.insert(word, val);
128+
}
129+
map.into_iter()
130+
.max_by_key(|&(_, v)| v)
131+
.unwrap()
132+
.0
133+
.to_string()
134+
}
135+
}
136+
```
137+
93138
### **...**
94139

95140
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::{HashMap, HashSet};
2+
impl Solution {
3+
pub fn most_common_word(mut paragraph: String, banned: Vec<String>) -> String {
4+
paragraph.make_ascii_lowercase();
5+
let banned: HashSet<&str> = banned.iter().map(String::as_str).collect();
6+
let mut map = HashMap::new();
7+
for word in paragraph.split(|c| !matches!(c, 'a'..='z')) {
8+
if word.is_empty() || banned.contains(word) {
9+
continue;
10+
}
11+
let val = map.get(&word).unwrap_or(&0) + 1;
12+
map.insert(word, val);
13+
}
14+
map.into_iter()
15+
.max_by_key(|&(_, v)| v)
16+
.unwrap()
17+
.0
18+
.to_string()
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function mostCommonWord(paragraph: string, banned: string[]): string {
2+
const s = paragraph.toLocaleLowerCase();
3+
const map = new Map<string, number>();
4+
const set = new Set<string>(banned);
5+
for (const word of s.split(/[^A-z]/)) {
6+
if (word === '' || set.has(word)) {
7+
continue;
8+
}
9+
map.set(word, (map.get(word) ?? 0) + 1);
10+
}
11+
return [...map.entries()].reduce(
12+
(r, v) => (v[1] > r[1] ? v : r),
13+
['', 0],
14+
)[0];
15+
}

0 commit comments

Comments
 (0)