Check if permutation of one string can break permutation of another
Last Updated :
22 Jul, 2021
Given two strings str1 and str2, the task is to check if any permutation of the given strings str1 and str2 is possible such that the character at each index of one string is greater than or equal to the other string.
Examples:
Input: A = “abc”, B = “xya”
Output: Yes
Explanation:
“ayx” is a permutation of B = “xya” which can break to string “abc” which is a permutation of A = “abc”.
Input: A = “abe”, B = “acd”
Output: “No”
Naive Approach: The idea is to generate all the permutation of one string and check if each character of any permutation is greater than the other string then print “YES” else print “NO”.
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Efficient Approach: Since we have to check if each character of permutation of one string is greater than or equals to the permutation of another string or not. The idea is to sort both the strings in alphabetical order. Now iterate a loop over all the character of the string if all the string of string str1 is less than str2 or all the character of string str2 is less than str1 then print Yes else print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool CanBreak(string A, string B)
{
sort(A.begin(), A.end());
sort(B.begin(), B.end());
bool ans1 = true , ans2 = true ;
for ( int i = 0; i < A.length(); i++) {
if (A[i] < B[i])
ans1 = false ;
if (B[i] < A[i])
ans2 = false ;
}
return ans1 || ans2;
}
int main()
{
string A = "abc" , B = "xya" ;
if (CanBreak(A, B))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
static boolean CanBreak(String A, String B)
{
A = sortString(A);
B = sortString(B);
boolean ans1 = true , ans2 = true ;
for ( int i = 0 ; i < A.length(); i++)
{
if (A.charAt(i) < B.charAt(i))
ans1 = false ;
if (B.charAt(i) < A.charAt(i))
ans2 = false ;
}
return ans1 || ans2;
}
static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
String A = "abc" , B = "xya" ;
if (CanBreak(A, B))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def CanBreak(A, B):
A = "".join( sorted (A))
B = "".join( sorted (B))
ans1 = True
ans2 = True
for i in range ( len (A)):
if (A[i] < B[i]):
ans1 = False
if (B[i] < A[i]):
ans2 = False
return (ans1 or ans2)
A = "abc"
B = "xya"
if (CanBreak(A, B)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool CanBreak(String A, String B)
{
A = sortString(A);
B = sortString(B);
bool ans1 = true , ans2 = true ;
for ( int i = 0; i < A.Length; i++)
{
if (A[i] < B[i])
ans1 = false ;
if (B[i] < A[i])
ans2 = false ;
}
return ans1 || ans2;
}
static String sortString(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
public static void Main(String[] args)
{
String A = "abc" , B = "xya" ;
if (CanBreak(A, B))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function CanBreak(A, B)
{
A = sortString(A);
B = sortString(B);
let ans1 = true , ans2 = true ;
for (let i = 0; i < A.length; i++)
{
if (A[i] < B[i])
ans1 = false ;
if (B[i] < A[i])
ans2 = false ;
}
return ans1 || ans2;
}
function sortString(inputString)
{
let tempArray = inputString.split( '' );
tempArray.sort();
return new String(tempArray);
}
let A = "abc" , B = "xya" ;
if (CanBreak(A, B))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...