Skip to content

Commit 4a8f99e

Browse files
add 819
1 parent c52c124 commit 4a8f99e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | O(m+n) | O(n) | |Easy| HashMap
2526
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | O(n) | O(n) | |Easy| HashMap
2627
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | O(n) | O(1) | |Easy|
2728
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | O(S) | O(S) | |Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
/**
10+
* 819. Most Common Word
11+
12+
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.
13+
It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
14+
Words in the list of banned words are given in lowercase, and free of punctuation.
15+
Words in the paragraph are not case sensitive. The answer is in lowercase.
16+
17+
Example:
18+
Input:
19+
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
20+
banned = ["hit"]
21+
Output: "ball"
22+
Explanation:
23+
"hit" occurs 3 times, but it is a banned word.
24+
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
25+
Note that words in the paragraph are not case sensitive,
26+
that punctuation is ignored (even if adjacent to words, such as "ball,"),
27+
and that "hit" isn't the answer even though it occurs more because it is banned.
28+
29+
Note:
30+
31+
1 <= paragraph.length <= 1000.
32+
1 <= banned.length <= 100.
33+
1 <= banned[i].length <= 10.
34+
The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
35+
paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
36+
Different words in paragraph are always separated by a space.
37+
There are no hyphens or hyphenated words.
38+
Words only consist of letters, never apostrophes or other punctuation symbols.
39+
*/
40+
public class _819 {
41+
public static class Solution1 {
42+
public String mostCommonWord(String paragraph, String[] banned) {
43+
Set<String> bannedSet = new HashSet(Arrays.asList(banned));
44+
String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
45+
Map<String, Integer> map = new HashMap<>();
46+
Arrays.stream(words)
47+
.filter(word -> !bannedSet.contains(word))
48+
.forEach(word -> map.put(word, map.getOrDefault(word, 0) + 1));
49+
String result = "";
50+
int freq = 0;
51+
for (String key : map.keySet()) {
52+
if (map.get(key) > freq) {
53+
result = key;
54+
freq = map.get(key);
55+
}
56+
}
57+
return result;
58+
}
59+
}
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._819;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _819Test {
10+
private static _819.Solution1 solution1;
11+
private static String[] banned;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _819.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
banned = new String[] {"hit"};
21+
assertEquals("ball",
22+
solution1.mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.",
23+
banned));
24+
}
25+
}

0 commit comments

Comments
 (0)