Code Lab

Build a word from letters

MediumStrings10 min

Write a function canBuildWord(letters, word) that checks if you can make word from the letters of letters.

  • Both arguments are strings of Latin letters. Either of them can be empty.
  • Every character of letters can be used only once: 'apple' has one e, so it cannot make 'peel'.
  • Case does not matter: 'A' and 'a' are the same letter.
  • Letters that are left over are fine.
  • An empty word can always be made.
  • Return a boolean: true or false.

Tip: a counter in a Map tells you how many times each letter is still free.

Examples

canBuildWord('listen', 'silent')

Returns true

canBuildWord('apple', 'peel')

Returns false

canBuildWord('Aab', 'aAB')

Returns true

Source

Loading the editor...
  1. InputcanBuildWord('listen', 'silent')
    Expectedtrue
  2. InputcanBuildWord('cat', 'dog')
    Expectedfalse
  3. InputcanBuildWord('abc', '')
    Expectedtrue

Hidden tests run on Submit.