A small letterpress shop keeps a single case of metal type: a fixed multiset of lowercase letter blocks that never gets restocked mid-shift. Customers drop off order slips, each a lowercase word they want typeset. A slip can be fulfilled only if the case currently holds at least as many blocks of every letter as that slip needs — for example, a slip containing two 'e's needs the case to contain at least two 'e' blocks. Slips are checked independently against the original, unchanging case (fulfilling one slip never consumes blocks that another slip also needs). Given the case's letters and the day's order slips, find the combined length of every slip that can be fully typeset.
chars — the letters currently sitting in the type case (lowercase English letters only).n — the number of order slips.n lines: one lowercase word each, the text of a single order slip.Print a single integer: the sum of the lengths of every slip that can be typeset entirely from chars.
chars <= 100Example 1
Input
printery 3 print tinier entry
Expected
10
Explanation
The case holds p:1, r:2, i:1, n:1, t:1, e:1, y:1. "print" needs p1,r1,i1,n1,t1 — all available, so it counts (length 5). "tinier" needs two i's but the case only has one, so it fails. "entry" needs e1,n1,t1,r1,y1 — all available, so it counts (length 5). Total = 5 + 5 = 10.
Example 2
Input
a 2 a aa
Expected
1
Explanation
The case holds a single 'a'. "a" needs one 'a', which is available, so it counts (length 1). "aa" needs two 'a's but only one is available, so it fails. Total = 1.
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 →