Check if permutaion of one string can break permutation of another

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 character at each index of one string is greater than or equal to the another 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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if permutation of
// one string can break permutation of
// another string or not
bool CanBreak(string A, string B)
{
    // Sort both the strings
    // in alphabetical order
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
 
    bool ans1 = true, ans2 = true;
 
    // Iterate over the strings
    for (int i = 0; i < A.length(); i++) {
 
        // If any of the string can break
        // other then mark ans as false;
        if (A[i] < B[i])
            ans1 = false;
        if (B[i] < A[i])
            ans2 = false;
    }
 
    // If any of the string can break
    return ans1 || ans2;
}
 
// Driver Code
int main()
{
    // Given string A and B
    string A = "abc", B = "xya";
 
    // Function Call
    if (CanBreak(A, B))
        cout << "Yes";
    else
        cout << "No";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if permutation of
// one String can break permutation of
// another String or not
static boolean CanBreak(String A, String B)
{
     
    // Sort both the Strings
    // in alphabetical order
    A = sortString(A);
    B = sortString(B);
 
    boolean ans1 = true, ans2 = true;
 
    // Iterate over the Strings
    for(int i = 0; i < A.length(); i++)
    {
         
        // If any of the String can break
        // other then mark ans as false;
        if (A.charAt(i) < B.charAt(i))
            ans1 = false;
        if (B.charAt(i) < A.charAt(i))
            ans2 = false;
    }
 
    // If any of the String can break
    return ans1 || ans2;
}
 
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char tempArray[] = inputString.toCharArray();
     
    // Sort tempArray
    Arrays.sort(tempArray);
     
    // Return new sorted string
    return new String(tempArray);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String A and B
    String A = "abc", B = "xya";
 
    // Function call
    if (CanBreak(A, B))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
 
// Function to check if permutation of
// one String can break permutation of
// another String or not
static bool CanBreak(String A, String B)
{
     
    // Sort both the Strings
    // in alphabetical order
    A = sortString(A);
    B = sortString(B);
 
    bool ans1 = true, ans2 = true;
 
    // Iterate over the Strings
    for(int i = 0; i < A.Length; i++)
    {
         
        // If any of the String can break
        // other then mark ans as false;
        if (A[i] < B[i])
            ans1 = false;
        if (B[i] < A[i])
            ans2 = false;
    }
 
    // If any of the String can break
    return ans1 || ans2;
}
 
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char []tempArray = inputString.ToCharArray();
     
    // Sort tempArray
    Array.Sort(tempArray);
     
    // Return new sorted string
    return new String(tempArray);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String A and B
    String A = "abc", B = "xya";
 
    // Function call
    if (CanBreak(A, B))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by gauravrajput1

chevron_right


Output: 

Yes


 

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

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : GauravRajput1