Check if String is Rotated

Practice this coding question on Coding Gurukul.

Problem statement

A circular display shifts a label by exactly two character positions during calibration. A technician receives the original and observed labels and checks whether the shift could have gone in either direction. Your task Determine whether b equals a rotated left by two positions or right by two positions. Important details Rotation wraps characters around the ends; it does not discard them. Compare letters with case preserved. The strings must have equal lengths. For length one or two, shifting twice returns the original string. A rotation by some other amount is insufficient unless it also matches one of the two permitted results. Worked example For a = amazon, rotating left twice gives azonam, while rotating right twice gives onamaz. Either observed label is accepted; amazon itself is not accepted for this input.

Input format

Two nonempty strings a and b on separate lines.

Output format

Print 1 if either two-position rotation matches, otherwise 0.

Constraints

1 <= length(a), length(b) <= 100000; strings contain English letters.

Sample input

amazon
azonam

Sample output

1

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…