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?
AnswerBot
1y

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

Help your peers!
Add answer anonymously...
Zopsmart Technology Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter