Skip to content

Commit c024bf8

Browse files
author
Tushar Borole
committed
819. Most Common Word
1 parent 375eefe commit c024bf8

File tree

3 files changed

+113
-38
lines changed

3 files changed

+113
-38
lines changed

.idea/workspace.xml

Lines changed: 41 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | |
1212
| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](self_dividing_numbers.js) | 68 ms | 38 MB | Easy | | | |
1313
| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Solution](sort_array_by_parity.js) | 84 ms | 37.3 MB | Easy | | | |
14+
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [most_common_word.js](most_common_word.js) | 76 ms | 37.8 MB | Easy | | | |
1415
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) | [Solution](robot_return_to_origin.js) | 80 ms | 41.3 MB | Easy | | | |
1516
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative](reverse_linked_list_iterative.js)<br>[Recursive](reverse_linked_list_recursive.js) | 64 ms <br> 64 ms | 35 MB <br> 35.4 MB | Easy | | | |
1617
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](fizz_buzz.js) | 72 ms | 37.4 MB | Easy | | | |

most_common_word.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 819. Most Common Word
2+
//
3+
// Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
4+
//
5+
// Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
6+
//
7+
//
8+
//
9+
// Example:
10+
//
11+
// Input:
12+
// paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
13+
// banned = ["hit"]
14+
// Output: "ball"
15+
// Explanation:
16+
// "hit" occurs 3 times, but it is a banned word.
17+
// "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
18+
// Note that words in the paragraph are not case sensitive,
19+
// that punctuation is ignored (even if adjacent to words, such as "ball,"),
20+
// and that "hit" isn't the answer even though it occurs more because it is banned.
21+
//
22+
//
23+
// Note:
24+
//
25+
// 1 <= paragraph.length <= 1000.
26+
// 0 <= banned.length <= 100.
27+
// 1 <= banned[i].length <= 10.
28+
// 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.)
29+
// paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
30+
// There are no hyphens or hyphenated words.
31+
// Words only consist of letters, never apostrophes or other punctuation symbols.
32+
33+
/**
34+
* @param {string} paragraph
35+
* @param {string[]} banned
36+
* @return {string}
37+
*/
38+
var mostCommonWord = function(paragraph, banned) {
39+
let set = new Set(banned);
40+
41+
let hashMap = {};
42+
43+
let array = paragraph
44+
.replace(/[!\?\.\,\'\;']/g, " ")
45+
.toLowerCase()
46+
.split(" ")
47+
.filter(a => a != ""); //?
48+
49+
for (let val of array) {
50+
if (!hashMap.hasOwnProperty(val)) hashMap[val] = 0;
51+
52+
hashMap[val]++;
53+
}
54+
55+
let sorted = Object.entries(hashMap).sort((a, b) => b[1] - a[1]);
56+
57+
for (let val of sorted) {
58+
if (!set.has(val[0])) {
59+
return val[0];
60+
}
61+
}
62+
};
63+
64+
mostCommonWord("a, a, a, a, b,b,b,c, c", ["hit"]); //?
65+
var object = {
66+
bob: 2,
67+
bose: 2,
68+
base: 1
69+
};
70+
71+
//Object.entries(object).sort((a, b) => b[1] - a[1]); //?

0 commit comments

Comments
 (0)