code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

819 Most Common Word

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * 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.
 * 
 * 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.
 */

function mostCommonWord(paragraph, banned) {
  let words = paragraph.toLowerCase().replace(/[!?',;\.]/g, ' ').trim().split(/\s+/g).filter(w => banned.indexOf(w) === -1);
  let map = {}
  for (let index = 0; index < words.length; index++) {
    map[words[index]] = (map[words[index]] || 0) + 1  
  }
  return Object.keys(map).sort((a,b) => map[b] - map[a])[0]
}

test('most Common Word', () => {
  expect(mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", ["hit"])).toEqual('ball')
});

Created 2020-04-18T21:41:31+00:00 · Edit