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 codeint main(){ string s1 = "ABACD"; string s2 = "CDABA"; cout << (isRotation(s1, s2) ? "1" : "0");}// This code is contributed by Chitranayal |
Java
// Java program to check if two strings are rotations// of each other.import java.util.*;import java.lang.*;import java.io.*;class stringMatching { public static boolean 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[] = new int[n]; // length of the previous longest prefix suffix int len = 0; int i = 1; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to n-1 while (i < n) { if (a.charAt(i) == b.charAt(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.charAt(k) != a.charAt(i++)) return false; } return true; } // Driver code public static void main(String[] args) { String s1 = "ABACD"; String s2 = "CDABA"; System.out.println(isRotation(s1, s2) ? "1" : "0"); }} |
C#
// C# program to check if // two strings are rotations// of each other.using System;class GFG{public static 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 = new int[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 codepublic static void Main(){ string s1 = "ABACD"; string s2 = "CDABA"; Console.WriteLine(isRotation(s1, s2) ? "1" : "0");}}// This code is contributed// by anuj_67. |
Output:
1
Time Complexity: O(n)
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- A Program to check if strings are rotations of each other or not
- Check if two numbers are bit rotations of each other or not
- Check if all rows of a matrix are circular rotations of each other
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check if two strings are permutation of each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings can be made equal by swapping one character among each other
- C Program to check if two given strings are isomorphic to each other
- Check if two given strings are isomorphic to each other
- Check whether two strings are anagram of each other
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Number of strings which starts and ends with same character after rotations
- Minimize count of given operations required to make two given strings permutations of each other
- Strings from an array which are not prefix of any other string
- Quickly find multiple left rotations of an array | Set 1
- Minimum rotations required to get the same String | Set-2
- Check if given string can be formed by two other strings or their permutations
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum sum of i*arr[i] among all rotations of a given array
- Count rotations divisible by 4
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

