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
letterscan be used only once:'apple'has onee, 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
wordcan always be made. - Return a boolean:
trueorfalse.
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
Loading the editor...
- Input
canBuildWord('listen', 'silent')Expectedtrue - Input
canBuildWord('cat', 'dog')Expectedfalse - Input
canBuildWord('abc', '')Expectedtrue
Hidden tests run on Submit