Skip to content

Commit 653d6f0

Browse files
committed
819_Most_Common_Word
1 parent ed934fc commit 653d6f0

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ Remember solutions are only solutions to given problems. If you want full study
139139
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
140140
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
141141
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
142-
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | String, Hash and Set. Set is recommended. |
142+
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
143+
| 819 | [Most Common Word](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
143144
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
144145

145146
| # | To Understand |

java/819_Most_Common_Word.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public String mostCommonWord(String paragraph, String[] banned) {
3+
paragraph += ".";
4+
5+
Set<String> banset = new HashSet();
6+
for (String word: banned) banset.add(word);
7+
Map<String, Integer> count = new HashMap();
8+
9+
String ans = "";
10+
int ansfreq = 0;
11+
12+
StringBuilder word = new StringBuilder();
13+
for (char c: paragraph.toCharArray()) {
14+
if (Character.isLetter(c)) {
15+
// word
16+
word.append(Character.toLowerCase(c));
17+
} else if (word.length() > 0) {
18+
// punctuation symbols
19+
// node that word length should be larger than 0
20+
String finalword = word.toString();
21+
if (!banset.contains(finalword)) {
22+
count.put(finalword, count.getOrDefault(finalword, 0) + 1);
23+
// Record max here
24+
if (count.get(finalword) > ansfreq) {
25+
ans = finalword;
26+
ansfreq = count.get(finalword);
27+
}
28+
}
29+
word = new StringBuilder();
30+
}
31+
}
32+
return ans;
33+
}
34+
}

python/819_Most_Common_Word.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
# https://leetcode.com/problems/most-common-word/discuss/193268/python-one-liner
9+
banned = set(banned)
10+
count = collections.Counter(word for word in re.split('[ !?\',;.]',
11+
paragraph.lower()) if word)
12+
return max((item for item in count.items() if item[0] not in banned),
13+
key=operator.itemgetter(1))[0]

0 commit comments

Comments
 (0)