Show problems as
Classic problem #383EasyHash TableStringCounting

Ransom Note

Real-world version: Spell It With Tiles
Tracked in this browser only
Read Ransom Note on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A signmaker's kit holds a jumble of engraved letter tiles. The string tiles lists what the kit contains, one lowercase character per tile, in no particular arrangement. A customer orders the label word, which has to be laid along a display rail exactly as written.

Every tile is a physical piece, so a tile placed on the rail is no longer in the kit; a label needing the same letter three times needs three separate tiles carrying it. Tiles left over in the kit are fine and the order they sit in never matters.

Report true when the kit can cover the whole label, and false when it runs short.

Examples

Example 1

Input
word = "kite", tiles = "tickle"
Output
true

The kit carries a k, an i, a t and an e, which is everything the label asks for.

Example 2

Input
word = "peer", tiles = "prime"
Output
false

The label wants two e tiles and the kit only holds one, so it runs short.

Example 3

Input
word = "cat", tiles = "tactic"
Output
true

Three of the six tiles cover the label and the leftovers stay in the kit.

Constraints

  • 1 <= word.length, tiles.length <= 10^5
  • word and tiles hold lowercase English letters only.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def can_construct(word: str, tiles: str) -> bool:
Java
public boolean canConstruct(String word, String tiles)