File tree 2 files changed +25
-3
lines changed
2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 1
1
# LeetCode Algorithms
2
2
3
- ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-190 /2081-1f425f.svg )
4
- ![ problems-solved-java] ( https://img.shields.io/badge/Java-190 /2081-1abc9c.svg )
3
+ ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-191 /2081-1f425f.svg )
4
+ ![ problems-solved-java] ( https://img.shields.io/badge/Java-191 /2081-1abc9c.svg )
5
5
![ problems-solved-python] ( https://img.shields.io/badge/Python-186/2081-1abc9c.svg )
6
6
[ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg )] ( CONTRIBUTING.md )
7
7
[ ![ cp] ( https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg )] ( https://github.com/anishLearnsToCode/competitive-programming )
220
220
| 806 | [ Number of Lines to Write String] ( https://leetcode.com/problems/number-of-lines-to-write-string ) | [ ![ Java] ( assets/java.png )] ( src/NumberOfLinesToWriteInString.java ) |
221
221
| 811 | [ Subdomain Visit Count] ( https://leetcode.com/problems/subdomain-visit-count ) | |
222
222
| 812 | [ Largest Triangle Area] ( https://leetcode.com/problems/largest-triangle-area ) | [ ![ Java] ( assets/java.png )] ( src/LargestTriangleArea.java ) |
223
- | 819 | [ Most Common Word] ( https://leetcode.com/problems/most-common-word ) | |
223
+ | 819 | [ Most Common Word] ( https://leetcode.com/problems/most-common-word ) | [ ![ Java ] ( assets/java.png )] ( src/MostCommonWord.java ) |
224
224
| 821 | [ Shortest Distance to Character] ( https://leetcode.com/problems/shortest-distance-to-a-character ) | |
225
225
| 824 | [ Goat Latin] ( https://leetcode.com/problems/goat-latin ) | |
226
226
| 830 | [ Positions of Large Groups] ( https://leetcode.com/problems/positions-of-large-groups ) | |
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+ import java .util .Collections ;
3
+ import java .util .HashMap ;
4
+ import java .util .HashSet ;
5
+ import java .util .Map ;
6
+ import java .util .Set ;
7
+
8
+ public class MostCommonWord {
9
+ public String mostCommonWord (String paragraph , String [] banned ) {
10
+ String text = paragraph .replaceAll ("[^a-zA-Z0-9 ]" , " " ).toLowerCase ();
11
+ Set <String > bannedWords = new HashSet <>(Arrays .asList (banned ));
12
+ Map <String , Integer > wordFrequencies = new HashMap <>();
13
+
14
+ for (String word : text .split ("\\ s+" )) {
15
+ if (!bannedWords .contains (word )) {
16
+ wordFrequencies .put (word , wordFrequencies .getOrDefault (word , 0 ) + 1 );
17
+ }
18
+ }
19
+
20
+ return Collections .max (wordFrequencies .entrySet (), Map .Entry .comparingByValue ()).getKey ();
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments