File tree 5 files changed +99
-0
lines changed
algorithms/MostCommonWord
5 files changed +99
-0
lines changed Original file line number Diff line number Diff line change @@ -133,6 +133,7 @@ All solutions will be accepted!
133
133
| 504| [ Base 7] ( https://leetcode-cn.com/problems/base-7/description/ ) | [ java/py/js] ( ./algorithms/Base7 ) | Easy|
134
134
| 830| [ Positions Of Large Groups] ( https://leetcode-cn.com/problems/positions-of-large-groups/description/ ) | [ java/py/js] ( ./algorithms/PositionsOfLargeGroups ) | Easy|
135
135
| 572| [ Subtree Of Another Tree] ( https://leetcode-cn.com/problems/subtree-of-another-tree/description/ ) | [ java/py/js] (./algorithms/Subtree Of Another Tree)| Easy|
136
+ | 819| [ Most Common Word] ( https://leetcode-cn.com/problems/most-common-word/description/ ) | [ java/py/js] ( ./algorithms/MostCommonWord ) | Easy|
136
137
137
138
# Database
138
139
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Most Common Word
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String mostCommonWord (String paragraph , String [] banned ) {
3
+ Map <String , Integer > wordsMap = new HashMap <String , Integer >();
4
+ List <String > words = new ArrayList <String >();
5
+ String res = null ;
6
+ int maxCount = 0 ;
7
+
8
+ paragraph = paragraph .toLowerCase ();
9
+ paragraph = paragraph .replaceAll ("[!?',;.]" , "" );
10
+
11
+ for (int i = 0 ; i < banned .length ; i ++) {
12
+ banned [i ] = banned [i ].toLowerCase ();
13
+ }
14
+
15
+ for (String word : paragraph .split (" " )) {
16
+ if (wordsMap .get (word ) == null ) {
17
+ wordsMap .put (word , 1 );
18
+ words .add (word );
19
+ } else {
20
+ wordsMap .put (word , wordsMap .get (word ) + 1 );
21
+ }
22
+ }
23
+
24
+ for (int i = 0 ; i < words .size (); i ++) {
25
+ String word = words .get (i );
26
+ if (!Arrays .asList (banned ).contains (word ) && wordsMap .get (word ) > maxCount ) {
27
+ res = word ;
28
+ maxCount = wordsMap .get (word );
29
+ }
30
+ }
31
+
32
+ return res ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } paragraph
3
+ * @param {string[] } banned
4
+ * @return {string }
5
+ */
6
+ var mostCommonWord = function ( paragraph , banned ) {
7
+ let wordsMap = { } ,
8
+ words = [ ] ,
9
+ res = null ,
10
+ maxCount = 0
11
+
12
+ paragraph = paragraph . toLowerCase ( )
13
+ paragraph = paragraph . replace ( / [ ! ? ' , ; . ] / g, '' )
14
+ banned = banned . map ( b => b . toLowerCase ( ) )
15
+
16
+ paragraph . split ( ' ' ) . forEach ( word => {
17
+ if ( wordsMap [ word ] === undefined ) {
18
+ wordsMap [ word ] = 1
19
+ words . push ( word )
20
+ } else {
21
+ wordsMap [ word ] ++
22
+ }
23
+ } )
24
+
25
+ words . forEach ( word => {
26
+ if ( banned . findIndex ( b => b === word ) === - 1 && wordsMap [ word ] > maxCount ) {
27
+ res = word
28
+ maxCount = wordsMap [ word ]
29
+ }
30
+ } )
31
+ return res
32
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def mostCommonWord (self , paragraph , banned ):
3
+ """
4
+ :type paragraph: str
5
+ :type banned: List[str]
6
+ :rtype: str
7
+ """
8
+ words_map = {}
9
+ words = []
10
+ paragraph = paragraph .lower ()
11
+ paragraph = re .sub (r"[!?',;.]" , '' , paragraph )
12
+
13
+ for i in range (len (banned )):
14
+ banned [i ] = banned [i ].lower ()
15
+
16
+ for word in paragraph .split (' ' ):
17
+ if words_map .get (word ) == None :
18
+ words_map [word ] = 1
19
+ words .append (word )
20
+ else :
21
+ words_map [word ] += 1
22
+
23
+ res = None
24
+ max_count = 0
25
+ for word in words :
26
+ if word not in banned and words_map [word ] > max_count :
27
+ res = word
28
+ max_count = words_map [word ]
29
+
30
+ return res
You can’t perform that action at this time.
0 commit comments