-
Notifications
You must be signed in to change notification settings - Fork 0
/
819_Most Common Word.js
40 lines (37 loc) · 977 Bytes
/
819_Most Common Word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// https://leetcode.com/problems/most-common-word/description/
/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
var mostCommonWord = function (paragraph, banned) {
let splitChars = "!?',;. ";
let words = {};
let word = '';
for (let i = 0; i < paragraph.length; i++) {
let char = paragraph[i];
if (splitChars.indexOf(char) === -1) {
word += char.toLowerCase();
} else {
if (word && banned.indexOf(word) === -1) {
words[word] = words[word] || 0;
words[word]++;
}
word = '';
}
}
let result = '';
words[result] = -1;
for (let key in words) {
if (words[key] > words[result]) {
result = key;
}
}
return result || word;
};
var paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
var banned = ["hit"];
console.log(mostCommonWord(paragraph, banned));
var paragraph = "Bob";
var banned = [];
console.log(mostCommonWord(paragraph, banned));