A kitchen keeps n identical spice jars in a row. Jar i currently holds a string of lowercase letters, where each letter represents one unit of a specific spice already placed inside that jar (e.g. 'c' for a unit of cinnamon). A prep cook is allowed to move any single spice unit from any jar into any other jar, any number of times (jars are not required to keep their original size). Determine whether the cook can reach a state where all n jars end up holding exactly the same blend, meaning that after sorting each jar's letters alphabetically, every jar's resulting string is identical.
Example 1
Input
2 aaab ba
Expected
true
Explanation
Combined counts: 'a' appears 3+1=4 times, 'b' appears 1+1=2 times. With n=2 jars, both 4 and 2 are divisible by 2, so it is possible, e.g. redistribute to "aab" and "aab" (two a's and one b each). Output: true.
Example 2
Input
2 aab c
Expected
false
Explanation
Combined counts: a=2, b=1, c=1, with n=2 jars. Letter 'b' has total count 1, which is not divisible by 2, so the b's cannot be split evenly between the two jars. Output: false.
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 →