Rotational Equivalence of Strings Problem Statement
Given two strings 'P' and 'Q' of equal length, determine if string 'P' can be transformed into string 'Q' by cyclically rotating it to the right any number of times (possibly zero).
Explanation:
A cyclic right rotation involves moving the rightmost character of string A to the leftmost position. For instance, if A = "pqrst", then it will become "tpqrs" after one right rotation.
Example:
Input:
P = "abfyg", Q = "gabfy"
Output:
1
Explanation:
If we cyclically rotate String P to the right once, it becomes "gabfy", which matches String Q. Thus, it is possible to convert String P to String Q.
Input:
The first line of the input contains an integer 'T', denoting the number of test cases. The first line of each test case contains the String 'P'. The second line of each test case contains the String 'Q'.
Output:
For each test case, print 1 if String 'P' can be cyclically rotated to form String 'Q', otherwise print 0. Print the result for each test case on a new line.
Constraints:
- 1 ≤ T ≤ 10
- 1 ≤ |P|, |Q| ≤ 105
- |P| = |Q|
- Strings 'P' and 'Q' consist only of lowercase English letters.
- Time Limit: 1 sec
Note:
You do not need to print anything; it is already handled. Just implement the given function.
Follow Up:
Can you solve this in O(N) time?
The task is to check if one string can be converted into another string by cyclically rotating it to the right any number of times.
Check if the lengths of the two strings are equal. If not, return 0.
C...read more
Top Zopsmart Technology Full Stack Developer interview questions & answers
Popular interview questions of Full Stack Developer
Reviews
Interviews
Salaries
Users/Month