Check if string can be made lexicographically smaller by reversing any substring


In C++ we have an inbuild reverse() function that will be used for reversing the substring to check whether a string can be made lexicographically smaller or not. Lexicographic ordering is the process by which the character of a word is sorted in a dictionary.

Let’s take an example of a string to check lexicographically smaller or not.

  • We will compare the two words to check the lexicographically smaller ones and take two strings namely ‘apple’ and ‘army’. Both these strings of the first letter starting with letter ‘a’. When we move to check the second character of both letters, then ‘p’ come before ‘r’ alphabetically. Therefore, apple is lexicographically smaller than the army.

  • In the string “tutorialspoint” reverse the substring “oria” to get “airo” which is lexicographically smaller. Then the final string is written as “tutairolspoint”.

  • In the string “tutorix” reverse the substring “tori” to get “irot” which is lexicographically smaller as the start of the first substring is ‘t’ and the second substring is ‘i’. Then ‘i’ comes before ‘t’ alphabetically and hence it makes ‘irot’ lexicographically smaller than ‘tori’. Then the final string is written as “tuirotx”

We will take another example of a string such as “acwz”.

Syntax

reverse( str_name.begin(), str_name.end() )

Explanation

The reverse function is part of C++ standard library and takes two parameters “str_name.begin()” and “str_name.end()”.

  • str_name is the name of the string suggested by the user.

  • begin() and end() is the predefined in-built function that is used under the reverse function. The work of begin function is that it returns an iterator pointing to the first character of the input string. The work of the end function is that it returns an iterator pointing to one position previous to the last character of the input string.

Note that the reverse function does not return anything as it modifies the container(str_name) in place.

Algorithm

  • First of all, we will use three necessary header files namely iostream, string, and include<algorithm, and also declare the std namespace.

  • We will start the program with the main function where we declare a string variable named ‘str’ and store the string, ‘tutorialspoint’ in it. Then we will declare the boolean variable ‘isReverse’ to ‘false’ to indicate that the given string is not reversed and is in original form.

  • Then we will create two nested for loops to check every possible substring of ‘str’. Then store the substring in the temporary string named ‘temp’.

  • After that we call the ‘reverse()’ function to reverse the substring stored in ‘temp’ variable between indexes ‘i’ and ‘j’.

  • Later on create an if statement to check whether the reversed string is lexicographically smaller by comparing the variable ‘temp’ and ‘str’.

  • The compiler will compare the variable temp and str. If they both are equal then the variable ‘isReverse’ will be set to true and it will break the if statement.

  • Now, we will check the value of isReverse, if it is true, it will print the statement of if-condition otherwise print the statement of else condition.

  • Exit the program.

Example

In this program, we are going to understand how string can be made lexicographically smaller by reversing any substring.

#include <iostream>
#include <string>
#include <algorithm> 
using namespace std;
int main() {
   string str = "tutorialspoint";
   // User can change this to test other strings such as acwz, groffer, etc
   bool isReverse = false; // it is used to indicate whether or not a substring has been found in the original string “str”.


   // use the loop through all possible substrings of str
   for (int i = 0; i < str.length(); i++) {
      for (int j = i+1; j < str.length(); j++) {
         string temp = str; 
         // create new temporary variable to store the value of str.
         // reverse the substring of i and j
         reverse ( temp.begin() + i, temp.begin() + j + 1 ); 
         // reverse function follow the header file name as <algorithm>
         if (temp < str) { 
            // Check whether the reversed string is lexicographically smaller
            isReverse = true;
            break;
         }
      }
   }
   if ( isReverse ) {
      cout << "Yes, this is lexicographically smaller by reversing a  substring." << endl;
   } else {
      cout << "No, this is not lexicographically smaller by reversing a substring." << endl;
   }
   return 0;
}

Output

If we enter the value “tutorialspoint”, we get the following result −

Yes, this is lexicographically smaller by reversing a  substring.

If we enter the value “acwz”, we get the following result:

No, this is not lexicographically smaller by reversing a substring.

Conclusion

We saw how we take the string variable to calculate the lexicographically smaller by reversing any substring. Then we set the string variable to a temporary variable. Then we used the predefined function “reverse()” to calculate the lexicographical word in reverse form. Next, we saw by comparing the variable- temp, and str to check the lexicographically smaller and get the result.

Updated on: 20-Apr-2023

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements