File tree Expand file tree Collapse file tree 4 files changed +125
-0
lines changed
solution/0800-0899/0819.Most Common Word Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -99,6 +99,51 @@ class Solution {
99
99
}
100
100
```
101
101
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
+
102
147
### ** ...**
103
148
104
149
```
Original file line number Diff line number Diff line change @@ -90,6 +90,51 @@ class Solution {
90
90
}
91
91
```
92
92
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
+
93
138
### ** ...**
94
139
95
140
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments