Given a string consisting only of two lowercase characters
Input: str = "yxxyx", p = 3, q = 3 Output: xyxyx Input: str = "yxxy", p = 3, q = 2 Output: Impossible
Approach: First of all, by induction it can prove that the product of count of ‘x’ and count of ‘y’ should be equal to the sum of the count of a subsequence of ‘xy’ and ‘yx’ for any given string. If this does not hold then the answer is ‘Impossible’ else answer always exist.
Now, sort the given string so the count of a subsequence of ‘yx’ becomes zero. Let nx be the count of ‘x’ and ny be count of ‘y’. let a and b be the count of subsequence ‘xy’ and ‘yx’ respectively, then a = nx*ny and b = 0. Then, from beginning of the string find the ‘x’ which has next ‘y’ to it and swap both untill you reach end of the string. In each swap a is decremented by 1 and b is incremented by 1. Repeat this until the count of a subsequence of ‘yx’ is achieved i:e a becomes p and b becomes q.
Below is the implementation of the above approach:
Time Complexity: O(N2)
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.
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically n-th permutation of a string
- Find n-th lexicographically permutation of a string | Set 2
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- K-th lexicographically smallest unique substring of a given string
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest string which differs from given strings at exactly K indices
- Lexicographically smallest subsequence possible by removing a character from given string
- Lexicographically smallest string possible by inserting given character
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- How to find Lexicographically previous permutation?
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.