Problem 212789 · easy · Phase 02 Linear Data Structures

Spell It from the Tiles

strings · counting · anagrams

You have a bag of letter tiles and want to spell a phrase. Write can_spell(phrase, tiles) that returns True if the letters in tiles are enough to spell phrase. Each tile can be used once. Spaces in the phrase need no tile. Letters are case-insensitive.

Examples

Input:  phrase = "hello", tiles = "olelh"
Output: True

Input:  phrase = "hello", tiles = "helo"
Output: False
Explanation: only one l tile.

Input:  phrase = "Hi there", tiles = "TEHREIH"
Output: True

Constraints

  • 0 <= len(phrase), len(tiles) <= 10**5, letters and spaces only
  • Target: O(n + m) time

Goals

  • Compare two multisets of letters
  • Ignore characters that need no tile
  • Normalise case consistently on both sides
Starting Python…