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.
Input: A = “abc”, B = “xya”
“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”
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:
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.
- 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
- Check if permutation of a given string can be made palindromic by removing at most K characters
- 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
- Permutation of an array that has smaller values from another array
- 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
- Check if any permutation of string is a K times repeated string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.