Two regional distributors of the same product catalog each keep a running sales ledger, where every sale is logged as a single lowercase letter naming which product sold, written in chronological order. Before doing a full reconciliation, head office wants a fast sanity check: the two ledgers are called near-mirrored if, for every product, the number of times it appears in one ledger differs from the number of times it appears in the other by at most three. You are given the two ledgers, guaranteed to be the same length. Decide whether they are near-mirrored.
Two lines. The first line contains ledgerA; the second line contains ledgerB.
Print true if the ledgers are near-mirrored, or false otherwise.
ledgerA == length of ledgerB <= 1000a-z.Example 1
Input
xxxxyyyy xxyyyyyy
Expected
true
Explanation
ledgerA has x:4, y:4. ledgerB has x:2, y:6. The difference for x is |4-2|=2 and for y is |4-6|=2; both are within the tolerance of 3, so the ledgers are near-mirrored and the answer is true.
Example 2
Input
zzzzzzzz zzzzpppp
Expected
false
Explanation
ledgerA has z:8. ledgerB has z:4, p:4. The difference for z is |8-4|=4, which already exceeds the tolerance of 3 (and p's difference |0-4|=4 exceeds it too), so the ledgers are not near-mirrored and the answer is 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 →