- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lexicographically minimum string rotation
Let us consider a string is given, we know that the string is a sequence of characters. The Lexicographical rotation is the rotation of string, to convert characters in lexicographical order.
The solution is simple, we simply concatenate the given string with itself, then in another array, all rotation of strings are stored. After that sort the array in ascending order, the lowest value is the final result.
Input and Output
Input: The String “BCAAFAABCD” Output: Rotated String: “AABCDBCAAF”
Algorithm
minStrRotation(str)
Input − The given string.
Output − Minimum string rotation required.
Begin n := length of str define strArr to store all rotations tempStr := concatenate str with str again for i := 0 to n, do strArr[i] := substring of tempStr from (i to n) done sort the strArr return strArr[0] End
Example
#include <iostream> #include <algorithm> using namespace std; string minStrRotation(string str) { int n = str.size(); string strArray[n]; //array to store all rotations of str string tempStr = str + str; //concatinate str two times for (int i = 0; i < n; i++) strArray[i] = tempStr.substr(i, n); //get sub string from ith index to nth index sort(strArray, strArray+n); return strArray[0]; //The first index is the result } int main() { string str; cout << "Enter String: "; cin >> str; cout << "Rotated String: " << minStrRotation(str); }
Output
Enter String: BCAAFAABCD Rotated String: AABCDBCAAF
- Related Articles
- Lexicographically Smallest Equivalent String in C++
- Program to find lexicographically smallest non-palindromic string in Python
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Program to find Lexicographically Smallest String With One Swap in Python
- Program to find lexicographically smallest string after applying operations in Python
- Queries to answer the X-th smallest sub-string lexicographically in C++
- Find the lexicographically smallest string which satisfies the given condition in Python
- Program to find kth smallest n length lexicographically smallest string in python
- Minimum String in C++
- Program to find lexicographically smallest string to move from start to destination in Python
- Java Program to check whether one String is a rotation of another.
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- Lexicographically next permutation in C++
- Check if a given string is a rotation of a palindrome in C++
- Finding minimum deletions in string in JavaScript

Advertisements