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:YesExplanation:

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

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

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if one string can be converted to another
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string d places
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another
- Find length of longest subsequence of one string which is substring of another string
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a string can be transformed to another by sorting substrings
- Count occurrences of a string that can be constructed from another given string
- Minimum number of given operations required to convert a permutation into an identity permutation
- Check if any permutation of string is a K times repeated string
- Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if characters of one string can be swapped to form other

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.