Skip to main content

Leetcode - 796. Rotate String

· One min read

https://leetcode.com/problems/most-common-word/

/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
var mostCommonWord = function (paragraph, banned) {
const onlyLowerCase = paragraph
.toLowerCase()
.split(/[ !?',;.]/)
.filter((val) => val !== '');
const hashTable = {};

onlyLowerCase.forEach((word) => {
if (!banned.includes(word)) {
hashTable[word] = hashTable[word] + 1 || 1;
}
});

const maxCount = Math.max(...Object.values(hashTable).map((value) => value));

return Object.keys(hashTable).find((key) => hashTable[key] === maxCount);
};

Runtime: 75 ms, faster than 90.68% of JavaScript online submissions for Most Common Word.

Memory Usage: 43.9 MB, less than 82.19% of JavaScript online submissions for Most Common Word.