Given two strings A and B of all uppercase letters, 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lexicographically smallest string formed by removing at most one character
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Find lexicographically smallest string in at most one swaps
- Find a string such that every character is lexicographically greater than its immediate next character
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Palindrome by swapping only one character
- Check if two strings can be made equal by swapping one character among each other
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Nearest smaller character to a character K from a Sorted Array
- Check if string can be made lexicographically smaller by reversing any substring
- Count of substrings having the most frequent character in the string as first character
- Print the lexicographically smallest array by swapping elements whose sum is odd
- 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
- Map every character of one string to another such that all occurrences are mapped to the same character
- Count of lexicographically smaller characters on right
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Lexicographically next greater string using same character set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.