C++ Program for Minimum rotations required to get the same string Last Updated : 28 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a string, we need to find the minimum number of rotations required to get the same string. Examples: Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1 The idea is based on below post.A Program to check if strings are rotations of each other or not Step 1: Initialize result = 0 (Here result is count of rotationsStep 2: Take a temporary string equal to original string concatenated with itself.Step 3: Now take the substring of temporary string of size same as original string starting from second character (or index 1).Step 4: Increase the count.Step 5: Check whether the substring becomes equal to original string. If yes, then break the loop. Else go to step 2 and repeat it from the next index. Below is the implementation of the above approach: C++ // C++ program to determine minimum number // of rotations required to yield same // string. #include <iostream> using namespace std; // Returns count of rotations to get the // same string back. int findRotations(string str) { // tmp is the concatenated string. string tmp = str + str; int n = str.length(); for (int i = 1; i <= n; i++) { // substring from i index of original // string size. string substring = tmp.substr(i, str.size()); // if substring matches with original string // then we will come out of the loop. if (str == substring) return i; } return n; } // Driver code int main() { string str = "abc"; cout << findRotations(str) << endl; return 0; } Output3 Time Complexity: O(n2)Auxiliary Space: O(n) Please refer complete article on Minimum rotations required to get the same string for more details! Comment More infoAdvertise with us Next Article C++ Program for Minimum rotations required to get the same string kartik Follow Improve Article Tags : Strings C++ Programs C++ DSA rotation +1 More Practice Tags : CPPStrings Similar Reads C++ Program to Find Lexicographically minimum string rotation | Set 1 Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.Source: Google Written TestMore Examples: Input: GEEKSQUIZ Output: EEKSQUIZG Input: GFG Output: FGG Input: GEEKSFORGEEKS Output: EEKSFORGEEKSG Following is a simple sol 2 min read C++ Program to Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc 3 min read C++ Program for Left Rotation and Right Rotation of a String Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : 5 min read C++ Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"} Output : 2 Explanation: In first string, we remove first element("m") from first 3 min read C++ Program for Queries for rotation and Kth character of the given string in constant time Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab 3 min read C++ Program for Longest subsequence of a number having same left and right rotation Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" 4 min read 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 Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings Given a numeric string target of length N and a set of numeric strings blocked, each of length N, the task is to find the minimum number of circular rotations required to convert an initial string consisting of only 0's to target by avoiding any of the strings present in blocked at any step. If not 11 min read C++ Program to check if strings are rotations of each other or not 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 = 2 min read C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 6 min read Like