C++ Program to check if strings are rotations of each other or not Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = str1.str1 2. If str2 is a substring of temp then str1 and str2 are rotations of each other. Example: str1 = "ABACD" str2 = "CDABA" temp = str1.str1 = "ABACDABACD" Since str2 is a substring of temp, str1 and str2 are rotations of each other. C++ // C++ program to check if two given strings // are rotations of each other # include <bits/stdc++.h> using namespace std; /* Function checks if passed strings (str1 and str2) are rotations of each other */ bool areRotations(string str1, string str2) { /* Check if sizes of two strings are same */ if (str1.length() != str2.length()) return false; string temp = str1 + str1; return (temp.find(str2) != string::npos); } /* Driver program to test areRotations */ int main() { string str1 = "AACD", str2 = "ACDA"; if (areRotations(str1, str2)) printf("Strings are rotations of each other"); else printf("Strings are not rotations of each other"); return 0; } Output: Strings are rotations of each other Time Complexity: O(n*n), where n is the length of the string. Auxiliary Space: O(N) where n is the length of the new string. Library Functions Used: strstr: strstr finds a sub-string within a string. Prototype: char * strstr(const char *s1, const char *s2); See http://www.lix.polytechnique.fr/Labo/Leo.Liberti/public/computing/prog/c/C/MAN/strstr.htm for more details strcat: strncat concatenate two strings Prototype: char *strcat(char *dest, const char *src); See http://www.lix.polytechnique.fr/Labo/Leo.Liberti/public/computing/prog/c/C/MAN/strcat.htm for more details Time Complexity: Time complexity of this problem depends on the implementation of strstr function. If implementation of strstr is done using KMP matcher then complexity of the above program is (-)(n1 + n2) where n1 and n2 are lengths of strings. KMP matcher takes (-)(n) time to find a substring in a string of length n where length of substring is assumed to be smaller than the string. Please refer complete article on A Program to check if strings are rotations of each other or not for more details! Comment More infoAdvertise with us Next Article C++ Program to check if strings are rotations of each other or not K kartik Follow Improve Article Tags : Strings C++ Programs C++ DSA rotation +1 More Practice Tags : CPPStrings Similar Reads 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 (lon 2 min read C++ Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 min read C++ Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1 Output: Yes, All rows are rotated permutationof each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No , As 3, 2, 1 is not a rotat 3 min read C++ Program To Check Whether Two Strings Are Anagram Of Each Other Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you 7 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 min read Like