Input: S1 = “hheftaabzzdr”, S2 = “earth”
Output: abdearthfhzz, zzhfearthdba
The smallest anagram of the given string S1 with S2 as a substring is “abdearthfhzz”
The largest anagram of the given string S1 with s2 as a substring is “zzhfearthdba”
Input: S1 = “ethgakagmenpgs”, S2 = “geeks”
Output: aageeksgghmnpt, tpmnhgggeeksaa
The smallest anagram of the given string S1 with S2 as a substring is “aageeksgghmnpt”
The largest anagram of the given string S1 with S2 as a substring is “tpmnhgggeeksaa”
Naive Approach: The simplest approach is to find all possible anagrams of S1 and check if any of those anagrams contain S2 as a substring or not. If yes, then find the lexicographically smallest and the largest among them.
Time Complexity: O(N!)
Auxiliary Space: O(N)
Efficient Approach: The idea is to first generate the lexicographically smallest anagram character by character and then find the lexicographically largest anagram by reversing the smallest anagram except for the substring which contains S2. Below are the steps:
- Initialize a map M and store the frequency of each character present in S1
- Maintain a Set S which stores the distinct characters present in S1.
- Decrease the frequency of characters of S1 from M which are already present in S2.
- Initialize an empty string res which will store the lexicographically largest anagram.
- Iterate over the set S, if the first character of string S2 is encountered while traversing the set values, check if the second distinct character of S2 is greater than the current character of Set. If so, then add all the characters of S2 to res.
- Otherwise, keep on iterating the Set and add the characters to res.
Below is the implementation of the above approach:
Time Complexity: O(N+M)
Auxiliary Space: O(N)
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 substring with maximum occurrences containing a's and b's only
- Lexicographically smallest K-length substring containing maximum number of vowels
- Lexicographically smallest and largest substring of size k
- Find the smallest window in a string containing all characters of another string
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Count of anagrams of each string in an array present in another array
- K-th lexicographically smallest unique substring of a given string
- Length of smallest substring of a given string which contains another string as subsequence
- Largest number from the longest set of anagrams possible from all perfect squares of length K
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimize length of prefix of string S containing all characters of another string T
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Shortest substring of a string containing all given words
- Check if string can be made lexicographically smaller by reversing any substring
- Count subsequences in first string which are anagrams of the second 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
- Find length of longest subsequence of one string which is substring of another 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.
Improved By : jyoti369