A logistics hub stamps every shipping container with a lowercase seal code. A container's seal strength is defined as the number of times the alphabetically earliest letter appearing anywhere in its code occurs in that code — for instance, the code bbto has alphabetically earliest letter b, which occurs twice, so its seal strength is 2; the code z has seal strength 1.
The hub keeps a reference vault holding the codes of n2 previously logged containers. A new batch of n1 incoming shipment codes has just arrived at the gate, and for every incoming code the inspector wants to know exactly how many vault codes have a seal strength that is strictly greater than that incoming code's own seal strength.
n1 and n2.n1 space-separated lowercase strings — the incoming shipment codes, in order.n2 space-separated lowercase strings — the vault codes.Print n1 integers separated by single spaces on one line. The i-th integer is the number of vault codes whose seal strength is strictly greater than the seal strength of the i-th incoming shipment code.
1 <= n1, n2 <= 20001 and 10 inclusive, and consists only of lowercase English letters a-z.Example 1
Input
2 4 cargo seal bin zoo aabb dock
Expected
2 2
Explanation
Seal strengths: cargo -> smallest letter 'a', occurs once -> 1; seal -> smallest letter 'a', occurs once -> 1. Vault: bin -> 'b' once -> 1; zoo -> 'o' twice -> 2; aabb -> 'a' twice -> 2; dock -> 'c' once -> 1. For query 'cargo' (strength 1), vault codes with strength strictly greater than 1 are 'zoo' and 'aabb', so the count is 2. The same holds for 'seal' (strength 1), giving 2 as well. Output: 2 2.
Example 2
Input
2 4 xyz aab aaa bb xyz aabbcc
Expected
3 1
Explanation
Seal strengths: xyz -> smallest letter 'x', occurs once -> 1; aab -> smallest letter 'a', occurs twice -> 2. Vault: aaa -> 'a' three times -> 3; bb -> 'b' twice -> 2; xyz -> 'x' once -> 1; aabbcc -> 'a' twice -> 2. For query 'xyz' (strength 1), vault codes with strength strictly greater than 1 are 'aaa'(3), 'bb'(2), and 'aabbcc'(2), giving 3. For query 'aab' (strength 2), only 'aaa'(3) is strictly greater, giving 1. Output: 3 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 →