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
4mo

Check if one string can be transformed into another by cyclically rotating it to the right.

  • Iterate through all possible rotations of string P and check if any of them match string Q.

  • Use string concate...read more

Help your peers!
Select
Add answer anonymously...

Microsoft Corporation Software Developer interview questions & answers

A Software Developer was asked 8mo agoQ. Given an integer array nums and an integer k, return the kth largest element in ...read more
A Software Developer was asked 8mo agoQ. Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0'...read more
A Software Developer was asked 9mo agoQ. Why do you want to join Microsoft?

Popular interview questions of Software Developer

A Software Developer was asked 8mo agoQ1. Given an integer array nums and an integer k, return the kth largest element in ...read more
A Software Developer was asked 8mo agoQ2. Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0'...read more
A Software Developer was asked 9mo agoQ3. Why do you want to join Microsoft?
Microsoft Corporation Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits