Given two strings A and B, the task is to find lexicographically the smallest permutation of string B such that it contains every substring from the string A as its substring. Print “-1” if no such valid arrangement is possible.
Input: A = “aa”, B = “ababab”
All possible substrings of A are (‘a’, ‘a’, ‘aa’)
Rearrange string B to “aaabb”.
Now “aaabb” is the lexicographically the smallest arrangement of B which contains all the substrings of A.
Input: A = “aaa”, B = “ramialsadaka”
All possible substrings of A are (‘a’, ‘aa’, ‘aaa’)
Rearrange string B to “aaaaadiklmrs”.
Now “aaaaadiklmrs” is the lexicographically smallest arrangement of B which contains all the substrings of A.
Naive Approach: The simplest approach is to generate all possible permutations of the string B and then from all these permutations, find lexicographically the smallest permutation which contains all substrings of A.
Time Complexity: O(N!)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the main observation is that the smallest string that contains all the substrings of A is the string A itself. Therefore, for string B to be reordered and contain all substring of A, it must contain A as a substring. The reordered string B can contain A as its substring only if the frequency of each character in string B is greater than or equal to its frequency in A. Below are the steps:
- Count the frequency of each character in string B in an array freq and then subtract from it the frequencies of the corresponding characters in string A.
- In order to form lexicographically the smallest string, initialize an empty string result and then append to it, all the leftover characters which are less in value than the first character of string A.
- Before appending all the characters equal to the first character A to result, check if there is any character which is less than the first character in string A. If so, then append A to result first and then all the remaining characters equal to the first character of A to make the reordered string lexicographically smallest.
- Otherwise, append all remaining occurrences of A and then append A.
- At last, append the remaining characters to the result.
- After the above steps, print the string result.
Below is the implementation of the above approach:
Time Complexity: O(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.
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest permutation of a string with given subsequences
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Length of smallest substring of a given string which contains another string as subsequence
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically all Shortest Palindromic Substrings from a given string
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically n-th permutation of a string
- Find n-th lexicographically permutation of a string | Set 2
- Find the smallest window in a string containing all characters of another string
- 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 smallest string formed by appending a character from first K characters of a string | Set 2
- Permutation of given string that maximizes count of Palindromic substrings
- How to find Lexicographically previous permutation?
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Check if a string can be transformed to another by sorting substrings
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.