Trains the technique from
LeetCode 242Valid AnagramThis 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 print shop sets words from loose metal type. The string tray lists the pieces of type sitting in a tray, one lowercase letter per piece, and the string target is the word a customer wants set.
The compositor may reorder the pieces freely but may not fetch a new piece or leave one out. Return true when the tray can be re-set to spell target exactly, and false otherwise.
Because each piece is a physical object, how many times a letter occurs matters: a tray holding two a pieces cannot set a word that needs three, even though both use the same letters.
Example 1
Both words need one c, one a, two l, one e and one r, so the same pieces serve either arrangement.
Example 2
The tray and the word draw on the same two letters, but the word calls for three b pieces and the tray holds two.
Example 3
Setting the word would leave a piece unused, which is not allowed.
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 can_respell(tray: str, target: str) -> bool:public boolean canRespell(String tray, String target)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.