#C9777. String Rotations: List All Rotations and Check Rotation
String Rotations: List All Rotations and Check Rotation
String Rotations: List All Rotations and Check Rotation
This problem requires you to perform operations on strings related to rotations. You will implement two functionalities:
- Listing All Rotations: Given a string S, generate all possible rotations of S. A rotation is obtained by taking a suffix of S and appending the prefix. For example, the rotations of abcdef are:
\(abcdef,\ bcdefa,\ cdefab,\ defabc,\ efabcd,\ fabcde\). - Rotation Check: Given two strings S1 and S2, determine if S2 is a rotation of S1. It can be shown mathematically that S2 is a rotation of S1 if and only if
\(S_2 \text{ is a substring of } (S_1+S_1)\).
You need to write a program that supports two modes. In mode 1, the program outputs all rotations of a given string. In mode 2, the program checks whether one string is a rotation of another.
inputFormat
The first line of input contains a single digit that determines the mode:
- If the digit is 1, then the next line contains a single string S. You must output all rotations of S.
- If the digit is 2, then the next two lines contain two strings S1 and S2 respectively. You must check if S2 is a rotation of S1.
All input should be read from stdin.
outputFormat
For mode 1, output a single line containing all rotations of the string separated by a space.
For mode 2, output a single line with either True
or False
(without quotes) indicating whether S2 is a rotation of S1.
All output should be written to stdout.
## sample1
abcdef
abcdef bcdefa cdefab defabc efabcd fabcde