Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmC++ Program to Check if strings are rotations of each other or...

C++ Program to Check if strings are rotations of each other or not | Set 2

Given two strings s1 and s2, check whether s2 is a rotation of s1. 
Examples: 

Input : ABACD, CDABA
Output : True

Input : GEEKS, EKSGE
Output : True

We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm’s lps (longest proper prefix which is also suffix) construction, which will help in finding the longest match of the prefix of string b and suffix of string a. By which we will know the rotating point, from this point match the characters. If all the characters are matched, then it is a rotation, else not.
Below is the basic implementation of the above approach. 
 

C++




// C++ program to check if 
// two strings are rotations
// of each other
#include<bits/stdc++.h>
using namespace std;
bool isRotation(string a, 
                string b)
{
  int n = a.length();
  int m = b.length();
  if (n != m)
    return false;
  
  // create lps[] that 
  // will hold the longest
  // prefix suffix values 
  // for pattern
  int lps[n];
  
  // length of the previous 
  // longest prefix suffix
  int len = 0;
  int i = 1;
    
  // lps[0] is always 0
  lps[0] = 0; 
  
  // the loop calculates 
  // lps[i] for i = 1 to n-1
  while (i < n) 
  {
    if (a[i] == b[len]) 
    {
      lps[i] = ++len;
      ++i;
    }
    else 
    {
      if (len == 0) 
      {
        lps[i] = 0;
        ++i;
      }
      else 
      {
        len = lps[len - 1];
      }
    }
  }
  
  i = 0;
  
  // Match from that rotating
  // point
  for (int k = lps[n - 1]; 
           k < m; ++k) 
  {
    if (b[k] != a[i++])
      return false;
  }
  return true;
}
  
// Driver code
int main()
{
  string s1 = "ABACD";
  string s2 = "CDABA";
  cout << (isRotation(s1, s2) ? 
           "1" : "0");
}
  
// This code is contributed by Chitranayal


Output: 
 

1

Time Complexity: O(n) 
Auxiliary Space: O(n)

Please refer complete article on Check if strings are rotations of each other or not | Set 2 for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
24 Jan, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments