Open In App

C++ Program to Check if a string can be obtained by rotating another string d places

Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right).

Examples: 

Input: str1 = “abcdefg”, str2 = “cdefgab”, d = 2 
Output: Yes 
Rotate str1 2 places to the left.

Input: str1 = “abcdefg”, str2 = “cdfdawb”, d = 6 
Output: No 
 

Approach: An approach to solve the same problem has been discussed here. In this article, reversal algorithm is used to rotate the string to the left and to the right in O(n). If any one of the rotations of str1 is equal to str2 then print Yes else print No.

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse an array from left
// index to right index (both inclusive)
void ReverseArray(string& arr, int left, int right)
{
    char temp;
    while (left < right) {
        temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}
 
// Function that returns true if str1 can be
// made equal to str2 by rotating either
// d places to the left or to the right
bool RotateAndCheck(string& str1, string& str2, int d)
{
 
    if (str1.length() != str2.length())
        return false;
 
    // Left Rotation string will contain
    // the string rotated Anti-Clockwise
    // Right Rotation string will contain
    // the string rotated Clockwise
    string left_rot_str1, right_rot_str1;
    bool left_flag = true, right_flag = true;
    int str1_size = str1.size();
 
    // Copying the str1 string to left rotation string
    // and right rotation string
    for (int i = 0; i < str1_size; i++) {
        left_rot_str1.push_back(str1[i]);
        right_rot_str1.push_back(str1[i]);
    }
 
    // Rotating the string d positions to the left
    ReverseArray(left_rot_str1, 0, d - 1);
    ReverseArray(left_rot_str1, d, str1_size - 1);
    ReverseArray(left_rot_str1, 0, str1_size - 1);
 
    // Rotating the string d positions to the right
    ReverseArray(right_rot_str1, 0, str1_size - d - 1);
    ReverseArray(right_rot_str1, str1_size - d, str1_size - 1);
    ReverseArray(right_rot_str1, 0, str1_size - 1);
 
    // Comparing the rotated strings
    for (int i = 0; i < str1_size; i++) {
 
        // If cannot be made equal with left rotation
        if (left_rot_str1[i] != str2[i]) {
            left_flag = false;
        }
 
        // If cannot be made equal with right rotation
        if (right_rot_str1[i] != str2[i]) {
            right_flag = false;
        }
    }
 
    // If both or any one of the rotations
    // of str1 were equal to str2
    if (left_flag || right_flag)
        return true;
    return false;
}
 
// Driver code
int main()
{
 
    string str1 = "abcdefg";
    string str2 = "cdefgab";
 
    // d is the rotating factor
    int d = 2;
 
    // In case length of str1 < d
    d = d % str1.size();
 
    if (RotateAndCheck(str1, str2, d))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Output: 

Yes

 

Time Complexity: O(n), where n represents the size of the given string.
Auxiliary Space: O(n), where n represents the size of the given string.

Approach – String Rotation Check

  • Check if the length of both strings is the same. If not, return false.
  • If d is greater than the length of the string, set d to d % length of string.
  • Create two temporary strings, left_rotate and right_rotate.
  • For left_rotate, copy the first d characters of the original string to the end of left_rotate, and then copy the remaining characters to the beginning of left_rotate.
  • For right_rotate, copy the last d characters of the original string to the beginning of right_rotate, and then copy the remaining characters to the end of right_rotate.
  • Check if either left_rotate or right_rotate is equal to the second string. If either one is equal, return true. Otherwise, return false.

C++




#include <iostream>
#include <string>
 
using namespace std;
 
bool canRotate(string str1, string str2, int d) {
   int n = str1.length();
 
   // Check if the length of both strings is the same
   if (n != str2.length()) {
       return false;
   }
 
   // If d is greater than the length of the string, set d to d % length of string
   d = d % n;
 
   // Create temporary strings for left and right rotations
   string left_rotate = str1.substr(d) + str1.substr(0, d);
   string right_rotate = str1.substr(n - d) + str1.substr(0, n - d);
 
   // Check if either left_rotate or right_rotate is equal to str2
   if (left_rotate == str2 || right_rotate == str2) {
       return true;
   }
 
   return false;
}
 
int main() {
   string str1 = "abcdefg";
   string str2 = "cdefgab";
   int d = 2;
 
   if (canRotate(str1, str2, d)) {
       cout << "Yes" << endl;
   } else {
       cout << "No" << endl;
   }
 
   return 0;
}


Output

Yes

The time complexity is O(n), where n represents the size of the given string.
The auxiliary space is O(n)

Please refer complete article on Check if a string can be obtained by rotating another string d places for more details!
 



Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads