A small print shop keeps a wooden case holding a jumble of individual metal letter blocks salvaged from a damaged forme. Every block carries exactly one lowercase letter, and the typesetter wants to lay the entire contents of the case out, in some order, to spell a requested line of text — using every block in the case exactly once, with nothing left over and nothing missing.
Given the letters currently sitting in the case and the line of text that needs to be set, determine whether the case's blocks can be rearranged to spell that line exactly.
caseLetters of lowercase English letters — the letters currently in the case.line of lowercase English letters — the line of text to be typeset.Print YES if the letters of caseLetters can be rearranged, using each block exactly once, to spell line exactly. Otherwise print NO.
caseLetters <= 10^5line <= 10^5Example 1
Input
stop pots
Expected
YES
Explanation
"stop" and "pots" both contain exactly the letters {p, o, s, t} once each, so the case's four blocks can be laid out in the order p-o-t-s to spell "pots" exactly, so the answer is YES.
Example 2
Input
hello world
Expected
NO
Explanation
"hello" contains the letters {h, e, l, l, o} while "world" contains {w, o, r, l, d}. The multisets differ (for example "hello" has two l's and no w, r, or d), so no rearrangement of the case can spell "world", so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →