C++ Program to Find Lexicographically minimum string rotation


The aim of this article is to implement a C++ Program to Find Lexicographically minimum string rotation.

Coming on to the definition of string, a string is a group of characters that ends with the null character "0" in C programming. Characters from the C String are kept in a character array. A C string differs from a character array in that it ends with the distinctive character "\0."

Finding the rotation of a string that has the lowest lexicographical order among all feasible rotations is known to be the lexicographically minimal string rotation as well as lexicographically smallest circular substring.

For instance, aaccaaddbb is the lexicographically minimum rotation of bbaaccaadd. Multiple lexicographically minimum rotations of a string are permitted, but they must all be equal.

Multiple lexicographically minimum rotations are conceivable given a string, but they must all be equivalent for the majority of applications. As a method of normalising strings, finding the lexicographically least rotation is helpful. It is possible to do a quick equality check if the strings reflect potentially isomorphic structures, such as graphs, by normalising them in this fashion.

Example 1

Let the given input string be
S : CATONTHEMAT
The output obtained here is : ATCATONTHEM

Example 2

Let the given input string be
S : TUTORIALSPOINT
The output obtained here is : ALSPOINTTUTORI

Example 3

Let the given input string be
S : ABCABCABC
The output obtained here is : ABCABCABC

Example 4

Let the given input string be
S : AAABBBCCC
The output obtained here is : AAABBBCCC

Problem Statement

Implement a C++ Program to Find Lexicographically minimum string rotation program.

Approach

The approach to solve this issue and come up with a C++ Program to Find Lexicographically minimum string rotatio goes like this.

This is a straightforward resolution. Let'str' be the string provided.

  • Join'str' to itself and save the result in the temporary string called 'concat'.

  • To store all of the rotations of "str," make an array of strings. Let's call the array 'arr'.

  • By using the substrings of "concat" at indexes 0, 1, 2, and n-1, find every rotation of "str". Put the rotations in arr[] storage.

  • Return arr[0] after sorting arr[].

Algorithm

The algorithm to implement a C++ Program to Find Lexicographically minimum string rotationgiven below

  • Step 1 − define a function to determine the rotation of the lexicographically smallest string

  • Step 2 − Define character array in which the lexicographic minimal string should be kept

  • Step 3 − Rotate the string one unit to the left.

  • Step 4 − Whether the rotation is currently at its minimum, update the result.

  • Step 5 − Print the result as the output.

Example: C++ program

Here is the C++ program implementation of the above written algorithm to obtain C++ Program to Find Lexicographically minimum string rotation.

Here we use <cstring> header file in order to more reliably monitor the data as well as the space it needs, CString keeps track of the entire length of character data.

#include <iostream>
#include <cstring>
void findLexMinRotation(char *inputStr) {
   char minRotation[100];
   strcpy(minRotation, inputStr);
   int length = strlen(inputStr);
   for (int i = 0; i < length; i++) {
      char temp = inputStr[0];
      memmove(inputStr, inputStr + 1, length - 1);
      inputStr[length - 1] = temp;
      if (strcmp(inputStr, minRotation) < 0) {
         strcpy(minRotation, inputStr);
      }
   }
   std::cout << "The result obtained from the input string " << inputStr << " is " << minRotation << std::endl;
}
int main() {
   char input[] = "TUTORIALSPOINT";
   findLexMinRotation(input);
   return 0;
}

Output

The result obtained from the input string TUTORIALSPOINT is ALSPOINTTUTORI

Conclusion

Likewise, we can obtain a C++ Program to Find Lexicographically minimum string rotation. The challenge of obtaining the C++ Program to Find Lexicographically minimum string rotation is resolved in this article.

Here the C++ programming code as well as the algorithm to implement a C++ Program to Find Lexicographically minimum string rotation and the methodology are provided.

Updated on: 30-Oct-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements