# Lexicographically smallest anagram of given string in range [L, R] for Q queries

• Last Updated : 22 Nov, 2021

Given a string S of size N and an array queries, containing Q queries in the form of L, R. The task is to find the lexicographically smallest anagram of string from L to R for each query.

Example:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: S = “bbaacd”
queries[]={{1, 3}, {2, 5}}
Output:
aab
aacd

Input: S=”bbdfaaacaed”
queries[]={{0, 4}, {4, 8}}
Output:
abbdf
aaaac

Approach: This question can be solved by precomputing the frequencies of all characters present till ith index in string S, because by doing so, it’s easier as well as time-saving to calculate the frequencies of characters between L to R, in each query. Now, to solve this question, follow the below steps:

• Create a function preComputeFreq, which will store the frequencies of characters in string S, for every index i.
• Create a function named smallestAnagram and run it for each query. In this function:
• Create a string ans, which will store the lexicographically smallest anagram from L to R.
• Find the frequencies, till index R and till index L-1, to get the frequencies between L to R.
• Run a loop from 0 to the frequency of that character and add it to the string ans.
• Return string ans as the answer for each query.
• Print the answer according to the above observation.

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach`` ` `#include ``using` `namespace` `std;`` ` `// Function to calculate the frequencies of all``// characters till each index in string``void` `preComputeFreq(string& S, ``                    ``vector >& freq)``{``    ``vector<``int``> f(26, 0);``    ``for` `(``int` `i = 0; i < S.size(); ++i) {``        ``freq[i] = f;``        ``freq[i][S[i] - ``'a'``]++;`` ` `        ``f = freq[i];``    ``}``}`` ` `// Function to get the``// smaleest anagram from L to R``string smallestAnagram(string& S, ``int` `L, ``int` `R,``                       ``vector >& freq)``{``    ``string ans;`` ` `    ``// Finding net frequencies``    ``// of character from L to R``    ``for` `(``int` `i = 0; i < 26; i++) {``        ``int` `low = 0;``        ``if` `(L > 0) {``            ``low = freq[L - 1][i];``        ``}`` ` `        ``// Adding characters to string ans``        ``for` `(``int` `j = 0; j < freq[R][i] - low; j++) {``            ``ans += (``char``)(``'a'` `+ i);``        ``}``    ``}`` ` `    ``return` `ans;``}`` ` `void` `smallestAnagramUtil(string& S, ``int` `N,``                         ``vector >& queries)``{``    ``vector > freq(N, vector<``int``>(26, 0));``    ``preComputeFreq(S, freq);`` ` `    ``for` `(``auto` `x : queries) {``        ``int` `L = x.first;``        ``int` `R = x.second;``        ``cout << smallestAnagram(S, L, R, freq) ``          ``<< endl;``    ``}``}`` ` `// Driver Code``int` `main()``{``    ``string S = ``"bbdfaaacaed"``;``    ``int` `N = S.size();``    ``vector > queries``        ``= { { 0, 4 }, { 4, 8 } };``    ``smallestAnagramUtil(S, N, queries);``}`
Output
```abbdf
aaaac
```

Time Complexity: O(Q*N)
Auxiliary Space: O(26*N)

My Personal Notes arrow_drop_up