Given two strings A and B of all uppercase letter, the task is to find whether is it possible to make string A strictly lexicographically smaller than string B by swapping at most one pair of characters in A.
Input: A = “AGAIN”, B = “ACTION”
We can make string A strictly lexicographically smaller than string B by swapping G and A (AAGIN)
AAGIN is lexicographically smaller than ACTION
Input: A = “APPLE” B = “AAAAAPPPLLE”
- Sort string A.
- We can find the first position where A and sorted(A) doesn’t match.
- We then find the letter that should be in that position and swap it with the letter in the sorted(A).
- If there are multiple choices, it is better to take the one that occurs last, since it makes the resulting string smallest.
- Now, compare string A and string B.
Below is the implementation of the above approach
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if string can be made lexicographically smaller by reversing any substring
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically next greater string using same character set
- Lexicographically smallest string formed by removing at most one character
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Print the lexicographically smallest array by swapping elements whose sum is odd
- Count of lexicographically smaller characters on right
- Palindrome by swapping only one character
- Check if two strings can be made equal by swapping one character among each other
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Lexicographically largest subsequence such that every character occurs at least k times
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Replace every character of string by character whose ASCII value is K times more than it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : abhaysingh290895