All problems
0147EasyHash TableStringCounting

Spell It With Tiles

Tracked in this browser only
Write code

Trains the technique from

LeetCode 383Ransom Note

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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