Java Program for Minimum rotations required to get the same string Last Updated : 26 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 rotations) Step 2 : Take a temporary string equals 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. Java // Java program to determine minimum number // of rotations required to yield same // string. import java.util.*; class GFG { // Returns count of rotations to get the // same string back. static 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.substring( i, i+str.length()); // if substring matches with original string // then we will come out of the loop. if (str.equals(substring)) return i; } return n; } // Driver Method public static void main(String[] args) { String str = "aaaa"; System.out.println(findRotations(str)); } } /* This code is contributed by Mr. Somesh Awasthi */ Output: 3 Time Complexity: O(n2) Please refer complete article on Minimum rotations required to get the same string for more details!Auxiliary Space: O(n), The extra space is used to store the copied string in tmp variable. Comment More infoAdvertise with us Next Article Java Program for Minimum rotations required to get the same string kartik Follow Improve Article Tags : Strings Java Java Programs DSA rotation +1 More Practice Tags : JavaStrings Similar Reads Java Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 min read Java 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 Java 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 Java Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl 3 min read Java 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 Java 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 : 6 min read Java 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 Java 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 Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read Java 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 Like