Spell It With Tiles

Easy · Arrays & Hashing · about 10 min

You have a bag of letter tiles, given as a string tiles (one character per tile). Can you lay out the string word using these tiles? Each tile can be used at most once, and you do not have to use every tile.

Return True (Python) / true (JavaScript) if you can, otherwise False / false.

  • Both strings contain only lowercase letters a to z and can be empty.
  • An empty word can always be spelled.

Examples:

can_spell_from_tiles("apple", "lppaex") → True

can_spell_from_tiles("apple", "aplex") → False (only one p tile)

Hints

  • Order does not matter, only how many of each letter you have.
  • Count the tiles first, then use them up one letter of the word at a time.
  • If you ever need a letter whose count is already zero, stop and answer no.

Learn the pattern first: the Arrays & Hashing lesson.

More Arrays & Hashing problems