Trains the technique from
LeetCode 1160Find Words That Can Be Formed by CharactersThis 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 sign shop keeps one tray of loose letter tiles, given as the string tray: each character of tray is one physical tile, so a letter appearing three times in tray means the shop owns three tiles of it.
A sign whose text is label can be cut when the tray holds enough tiles to spell it: every letter of label needs its own tile, so a label using a letter twice needs two tiles of that letter. Deciding whether one label can be cut never uses up the tray — each label in labels is judged against the full tray, and the same label may appear in labels more than once, in which case it counts each time.
Return the total number of tiles the shop would place across all the labels it can cut, which is the sum of the lengths of those labels.
Example 1
The tray stocks one `g`, two `o`, one `w`, one `l` and one `n`. `"glow"` uses four tiles and `"log"` uses three, and `"wool"` uses four including both `o` tiles, so those three labels contribute 4 + 3 + 4. `"gong"` needs two `g` tiles and the tray has one.
Example 2
The tray stocks two `p`, one `r` and two `e`. `"pepper"` needs three `p` tiles, which the tray cannot supply. `"per"` needs one of each and contributes its three tiles.
Example 3
The label wants two `z` tiles and the tray holds one, so no label can be cut and no tiles are placed.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def total_spellable_length(labels: list[str], tray: str) -> int:public int totalSpellableLength(String[] labels, String tray)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.