Check if any anagram of string S is lexicographically smaller than that of string T
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:
- 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.
C++
#include <bits/stdc++.h>
using namespace std;
void CompareAnagrams(string S, string T)
{
sort(S.begin(), S.end());
sort(T.begin(), T.end(), greater< char >());
if (S.compare(T) < 0) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
string S = "cd" ;
string T = "abc" ;
CompareAnagrams(S, T);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String ReverseString(String myStr)
{
String nstr = "" ;
char ch;
for ( int i = 0 ; i < myStr.length(); i++) {
ch = myStr.charAt(i);
nstr
= ch + nstr;
}
return nstr;
}
static String sortString(String str)
{
char [] arr = str.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
static void CompareAnagrams(String S, String T)
{
sortString(S);
T = sortString(T);
T = ReverseString(T);
if (S.compareTo(T) < 0 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static void main(String[] args)
{
String S = "cd" ;
String T = "abc" ;
CompareAnagrams(S, T);
}
}
|
Python3
def CompareAnagrams(S, T):
S = list (S)
S.sort()
S = ''.join(S)
T = list (T)
T.sort(reverse = True )
T = ''.join(T)
if (S < T):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
S = "cd"
T = "abc"
CompareAnagrams(S, T)
|
C#
using System;
public class GFG
{
static string ReverseString( string myStr)
{
char [] myArr = myStr.ToCharArray();
Array.Reverse(myArr);
return new string (myArr);
}
static void sortString(String str) {
char []arr = str.ToCharArray();
Array.Sort(arr);
String.Join( "" ,arr);
}
static void CompareAnagrams( string S, string T)
{
sortString(S);
sortString(T);
ReverseString(T);
if ( string .Compare(S, T) < 0) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
public static void Main(String []args) {
string S = "cd" ;
string T = "abc" ;
CompareAnagrams(S, T);
}
}
|
Javascript
<script>
function ReverseString(myStr)
{
let nstr = "" ;
let ch;
for (let i = 0; i < myStr.length; i++) {
ch = myStr[i];
nstr
= ch + nstr;
}
return nstr;
}
function sortString(str)
{
let arr = str.split();
arr.sort();
return arr.join();
}
function CompareAnagrams(S, T)
{
sortString(S);
T = sortString(T);
T = ReverseString(T);
if (S.localeCompare(T) < 0) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
let S = "cd" ;
let T = "abc" ;
CompareAnagrams(S, T);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Last Updated :
14 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...