 Open in App
Not now

# Check if any anagram of string S is lexicographically smaller than that of string T

• Last Updated : 14 Dec, 2021

Given two strings S and T, the task is to check if any anagram of string S is lexicographically smaller than any anagram of string T.

Example:

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:

1. Sort string S to get its lexicographically smallest anagram.
2. Reverse sort string T to get its lexicographically largest anagram.
3. 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.

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if any anagram``// of string S is lexicographically``// smaller than any anagram of string T``void` `CompareAnagrams(string S, string T)``{``    ``// Sort string S``    ``sort(S.begin(), S.end());` `    ``// Reverse sort string T``    ``sort(T.begin(), T.end(), greater<``char``>());`  `    ``// Comparing both the strings``    ``if` `(S.compare(T) < 0) {``        ``cout << ``"Yes"` `<< endl;``    ``}``    ``else` `{``        ``cout << ``"No"` `<< endl;``    ``}``}` `// Driver code``int` `main()``{``    ``string S = ``"cd"``;``    ``string T = ``"abc"``;` `    ``CompareAnagrams(S, T);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG``{``  ` `    ``// function to Reverse String``    ``static` `String ReverseString(String myStr)``    ``{``        ``String nstr = ``""``;``        ``char` `ch;` `        ``for` `(``int` `i = ``0``; i < myStr.length(); i++) {``            ``ch = myStr.charAt(i); ``// extracts each character``            ``nstr``                ``= ch + nstr; ``// adds each character in``                             ``// front of the existing string``        ``}` `        ``return` `nstr;``    ``}``  ` `    ``// function to print string in sorted order``    ``static` `String sortString(String str)``    ``{``        ``char``[] arr = str.toCharArray();``        ``Arrays.sort(arr);``        ``return` `new` `String(arr);``    ``}` `    ``// Function to check if any anagram``    ``// of string S is lexicographically``    ``// smaller than any anagram of string T``    ``static` `void` `CompareAnagrams(String S, String T)``    ``{` `        ``// Sort string S``        ``sortString(S);` `        ``// Reverse sort string T``        ``T = sortString(T);``        ``T = ReverseString(T);` `        ``// Comparing both the strings``        ``if` `(S.compareTo(T) < ``0``) {``            ``System.out.println(``"Yes"``);``        ``}``        ``else` `{``            ``System.out.println(``"No"``);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String S = ``"cd"``;``        ``String T = ``"abc"``;` `        ``CompareAnagrams(S, T);``    ``}``}` `// This code is contributed by Potta Lokesh`

## Python3

 `# Python 3 program for the above approach` `# Function to check if any anagram``# of string S is lexicographically``# smaller than any anagram of string T`  `def` `CompareAnagrams(S,  T):` `    ``# Sort string S``    ``S ``=` `list``(S)``    ``S.sort()``    ``S ``=` `''.join(S)` `    ``# Reverse sort string T``    ``T ``=` `list``(T)``    ``T.sort(reverse``=``True``)``    ``T ``=` `''.join(T)` `    ``# Comparing both the strings``    ``if` `(S < T):``        ``print``(``"Yes"``)` `    ``else``:``        ``print``(``"No"``)` `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``S ``=` `"cd"``    ``T ``=` `"abc"` `    ``CompareAnagrams(S, T)` `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG``{``  ` `// function to Reverse String``static` `string` `ReverseString(``string` `myStr)``    ``{``        ``char``[] myArr = myStr.ToCharArray();``        ``Array.Reverse(myArr);``        ``return` `new` `string``(myArr);``    ``}` `// function to print string in sorted order``    ``static` `void` `sortString(String str) {``        ``char` `[]arr = str.ToCharArray();``        ``Array.Sort(arr);``        ``String.Join(``""``,arr);``    ``}` `// Function to check if any anagram``// of string S is lexicographically``// smaller than any anagram of string T``static` `void` `CompareAnagrams(``string` `S, ``string` `T)``{``  ` `    ``// Sort string S``    ``sortString(S);` `    ``// Reverse sort string T``    ``sortString(T);``    ``ReverseString(T);``    `  `    ``// Comparing both the strings``    ``if` `(``string``.Compare(S, T) < 0) {``        ``Console.WriteLine(``"Yes"``);``    ``}``    ``else` `{``        ``Console.WriteLine(``"No"``);``    ``}``}` `// Driver Code``public` `static` `void` `Main(String []args) {``    ` `    ``string` `S = ``"cd"``;``    ``string` `T = ``"abc"``;` `    ``CompareAnagrams(S, T);``}``}` `// This code is contributed by target_2.`

## Javascript

 ``

Output

`No`

Time Complexity: O(N*logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up