Given two strings str1 and str2, the task is to find the lexicographic smallest permutation of str1 that contains str2 as a substring.
Note: Assume that the solution always exists.
Input: str1 = “abab”, str2 = “ab”
Explanation: Lexicographic smallest permutation of string str1 is “aabb”, Since “aabb” contains the string “ab” as a substing, therefore, “aabb” is the required answer.
Input: str1 = “geeksforgeeks”, str2 = “for”
Approach: This problem can be solved using the concept of Frequency Counting technique. Follow the steps below to solve this problem.
- Store frequency of all the characters of the strings str1 and str2.
- Initialize the resultant string with the substring.
- Subtract the frequency of the second string from the frequency of the first string
- Now, append the remaining characters which are lexicographically smaller than or equal to the first character of the substring before the substring in the resultant string.
- Append the remaining characters in the lexicographical order behind the substring in the resultant string.
- Finally, print the resultant string.
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.
- Find the N-th lexicographic permutation of string using Factoradic method
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest K-length substring containing maximum number of vowels
- Smallest non-zero substring which has any permutation divisible by 2^K
- Find the smallest and second smallest elements in an array
- Minimum changes required to make first string substring of second string
- Shortest substring of a string containing all given words
- Find the smallest window in a string containing all characters of another string
- Lexicographic rank of a string
- Lexicographic rank of a string using STL
- Generating distinct subsequences of a given string in lexicographic order
- Print a number as string of 'A' and 'B' in lexicographic order
- Lexicographic rank of a string among all its substrings
- Lexicographic rank of a string with duplicate characters
- Find a string in lexicographic order which is in between given two strings
- Python | Get the smallest window in a string containing all characters of given pattern
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Print all permutations in sorted (lexicographic) order
- Power Set in Lexicographic order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.