Code Lab

Modo entrevista. Cronômetro de 15 min, sem dicas e sem autocompletar

Count every word

DifícilMap and Set15 min

Write a function countWords(sentence) that counts how many times every word occurs.

  • Return a Map: the key is the word in lower case, the value is the count.
  • A word is a run of letters and digits (Latin and Cyrillic). Spaces and punctuation separate words.
  • Case does not matter: Test and test are the same word.
  • If there are no words, return an empty Map.
  • Key order is not checked.

Exemplos

countWords('the cat and the hat')

Retorna Map(4) { 'the' => 2, 'cat' => 1, 'and' => 1, 'hat' => 1 }

countWords('Test, test!')

Retorna Map(1) { 'test' => 2 }

Fonte

Carregando o editor...
  1. EntradacountWords('the cat and the hat')
    EsperadoMap(4) { 'the' => 2, 'cat' => 1, 'and' => 1, 'hat' => 1 }
  2. EntradacountWords('Test test TEST')
    EsperadoMap(1) { 'test' => 3 }
  3. EntradacountWords('')
    EsperadoMap(0) {}

Os testes ocultos rodam no envio