Given two strings S and T, the task is tocheck if any anagram of string S is lexicographically smaller than any anagram of string T.
Input: S = “xy”, T = “axy” Output: Yes Explanation: Rearrange yx into xy and axy into yxa. Then, xy<yxa.
Input: S = “cd”, T = “abc” Output: No
Approach: The approach is to check if the lexicographically smallest anagram of the string S is smaller than the lexicographically largest anagram of string T. If it is, then the answer is Yes. Otherwise, No. Now, follow the below steps to solve this question:
Sort string S to get its lexicographically smallest anagram.
Reverse sort string T to get its lexicographically largest anagram.
Check if the new string T is greater than the new string S or not. If it is, print Yes. Otherwise, print No.
Below is the implementation of the above approach.
Please Login to comment...