Skip to content
Related Articles

Related Articles

Improve Article

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++




// 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";
}

Java




// 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

Python3




# Python3 program for 
# the above approach
  
# Function to check if 
# permutation of one string 
# can break permutation of
# another string or not
def CanBreak(A, B):
  
    # Sort both the strings
    # in alphabetical order
    A = "".join(sorted(A))
    B = "".join(sorted(B))
    ans1 = True
    ans2 = True
  
    # Iterate over the strings
    for i in range(len(A)):
  
        # 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 or ans2)
  
# Driver Code
  
# Given string A and B
A = "abc"
B = "xya"
  
# Function Call
if(CanBreak(A, B)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by avanitrachhadiya2155

C#




// 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

Javascript




<script>
// JavaScript program for the above approach
  
// Function to check if permutation of
// one String can break permutation of
// another String or not
function CanBreak(A, B)
{
       
    // Sort both the Strings
    // in alphabetical order
    A = sortString(A);
    B = sortString(B);
   
    let ans1 = true, ans2 = true;
   
    // Iterate over the Strings
    for(let 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;
}
   
function sortString(inputString)
{
       
    // Convert input string to char array
    let tempArray = inputString.split('');
       
    // Sort tempArray
    tempArray.sort();
       
    // Return new sorted string
    return new String(tempArray);
}
  
// Driver Code    
      
    // Given String A and B
    let A = "abc", B = "xya";
   
    // Function call
    if (CanBreak(A, B))
        document.write("Yes");
    else
        document.write("No");
                    
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :