Java Program to Find Lexicographically minimum string rotation | Set 1 Last Updated : 27 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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 solution. Let the given string be 'str' 1) Concatenate 'str' with itself and store in a temporary string say 'concat'. 2) Create an array of strings to store all rotations of 'str'. Let the array be 'arr'. 3) Find all rotations of 'str' by taking substrings of 'concat' at index 0, 1, 2..n-1. Store these rotations in arr[] 4) Sort arr[] and return arr[0]. Following is the implementation of above solution. Java // A simple Java program to find // lexicographically minimum rotation // of a given String import java.util.*; class GFG { // This functionr return lexicographically // minimum rotation of str static String minLexRotation(String str) { // Find length of given String int n = str.length(); // Create an array of strings // to store all rotations String arr[] = new String[n]; // Create a concatenation of // String with itself String concat = str + str; // One by one store all rotations // of str in array. A rotation is // obtained by getting a substring of concat for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } // Sort all rotations Arrays.sort(arr); // Return the first rotation // from the sorted array return arr[0]; } // Driver code public static void main(String[] args) { System.out.println(minLexRotation("GEEKSFORGEEKS")); System.out.println(minLexRotation("GEEKSQUIZ")); System.out.println(minLexRotation("BCABDADAB")); } } // This code is contributed by 29AjayKumar Output: EEKSFORGEEKSG EEKSQUIZG ABBCABDAD Time complexity of the above solution is O(n2Logn) under the assumption that we have used a O(nLogn) sorting algorithm. Please refer complete article on Lexicographically minimum string rotation | Set 1 for more details! Comment More infoAdvertise with us Next Article Java Program to Find Lexicographically minimum string rotation | Set 1 K kartik Follow Improve Article Tags : Strings Java Java Programs DSA rotation lexicographic-ordering +2 More Practice Tags : JavaStrings Similar Reads Java Program for Minimum rotations required to get the same string 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 2 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 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 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 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 Like