A collector is restoring an antique cipher machine made of a row of rotating wheels, each engraved with the 26 lowercase letters arranged in a cycle so that rotating a wheel forward past 'z' wraps back to 'a'. Wheel i currently shows the letter s[i] and must end up showing the target letter t[i]. Restoration proceeds through a fixed sequence of numbered sessions 1, 2, ..., k. In session number j, the collector may pick at most one wheel that has never been touched in any earlier session and rotate it forward by exactly j notches (wrapping cyclically); a session may also be skipped entirely, and no wheel may ever be touched twice. Determine whether every wheel can be brought to its exact target letter by the end of session k.
Line 1: the string s (lowercase letters), the wheels' current letters. Line 2: the string t (lowercase letters, the same length as s), the wheels' target letters. Line 3: a single integer k, the number of numbered sessions available.
Print YES if every wheel can reach its target letter using at most k sessions under the stated rules, or NO otherwise.
Example 1
Input
ab bd 3
Expected
YES
Explanation
Wheel 0 needs a forward rotation of 1 notch (a to b), which is assigned to session 1. Wheel 1 needs a forward rotation of 2 notches (b to d), which is assigned to session 2. Both required session numbers (1 and 2) are at most k=3, so the answer is YES.
Example 2
Input
aa bb 1
Expected
NO
Explanation
Both wheels need a forward rotation of exactly 1 notch (a to b). Only session number 1 delivers a rotation of 1 notch modulo 26, and it can be used by just one wheel; the second wheel would need session number 1+26=27 to get the same net rotation, which exceeds k=1. 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 →