Given a string S, the task is to check if we can make the string lexicographically smaller by reversing any sub-string of the given string.
Examples:
Input: S = “striver”
Output: Yes
Reverse “rive” to get “stevirr” which is lexicographically smaller.Input: S = “rxz”
Output: No
Approach: Iterate in the string and check if for any index s[i] > s[i + 1]. If there exists at least one such index, then it is possible else not.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if s // can be made lexicographically smaller // by reversing a sub-string in s bool check(string s) { int n = s.size(); // Traverse in the string for (int i = 0; i < n - 1; i++) { // Check if s[i+1] < s[i] if (s[i] > s[i + 1]) return true; } // Not possible return false; } // Driver code int main() { string s = "geeksforgeeks"; if (check(s)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class GFG { // Function that returns true if s // can be made lexicographically smaller // by reversing a sub-string in s static boolean check(String s) { int n = s.length(); // Traverse in the string for (int i = 0; i < n - 1; i++) { // Check if s[i+1] < s[i] if (s.charAt(i) > s.charAt(i + 1)) return true; } // Not possible return false; } // Driver code public static void main(String args[]) { String s = "geeksforgeeks"; if (check(s)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Arnab Kundu |
Python3
# Python 3 implementation of the approach # Function that returns true if s # can be made lexicographically smaller # by reversing a sub-string in s def check(s): n = len(s) # Traverse in the string for i in range(n-1): # Check if s[i+1] < s[i] if (s[i] > s[i + 1]): return True # Not possible return False # Driver code if __name__ == '__main__': s = "geeksforgeeks" if (check(s)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if s // can be made lexicographically smaller // by reversing a sub-string in s static bool check(String s) { int n = s.Length; // Traverse in the string for (int i = 0; i < n - 1; i++) { // Check if s[i+1] < s[i] if (s[i] > s[i + 1]) return true; } // Not possible return false; } // Driver code public static void Main(String []args) { String s = "geeksforgeeks"; if (check(s)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the approach // Function that returns true if s // can be made lexicographically smaller // by reversing a sub-string in s function check($s) { $n = strlen($s); // Traverse in the string for ($i = 0; $i < $n - 1; $i++) { // Check if $s[$i+1] < $s[$i] if ($s[$i] > $s[$i + 1]) return true; } // Not possible return false; } // Driver code $s = "geeksforgeeks"; if (check($s)) echo "Yes"; else echo "No"; // This code is contributed by jit_t ?> |
Yes
Recommended Posts:
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smaller string by swapping at most one character pair
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Check if a two character string can be made using given words
- Check whether the string S1 can be made equal to S2 with the given operation
- K-th lexicographically smallest unique substring of a given string
- Count of lexicographically smaller characters on right
- String obtained by reversing and complementing a Binary string K times
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check whether two strings can be made equal by increasing prefixes
- Check whether an array can be made strictly increasing by modifying atmost one element
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if the rows of a binary matrix can be made unique by removing a single column
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if two strings can be made equal by swapping one character among each other
- Check if all bits can be made same by flipping two consecutive bits
- Check if all strings of an array can be made same by interchanging characters
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Find the longest string that can be made up of other strings from the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


