You are given two integers, βNβ and βVβ. You need to construct a string having length βNβ and numeric value βVβ.
The numeric value of a character is determined by its position in alphabets( 1- indexed)
For Example-
The numeric value of βaβ is 1, βbβ is 2.
The numeric value of a string is determined by adding the numeric value of all its characters.
For Example -
The numeric value of βbdeβ is 2 + 4 + 5, i.e. 11.
Example:
If 'N' = 3 and 'V' = 10, you need to construct string as βaah".
The first line contains a single integer βTβ, denoting the number of test cases.
Each test caseβs first line contains two space-separated integers, βNβ, denoting the stringβs length to construct, βVβ, denoting the stringβs required numeric value.
Output Format:
For each test case, return the lexicographically string having the length βNβ and numeric value βVβ.
Note:
You donβt need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^5
N <= V <= 26*N
Time Limit: 1 sec
2
5 29
3 42
aaaay
aoz
Test Case 1: The numeric value of βaaaayβ is 1+1+1+1+25
This is the shortest lexicographic string with length β5β and numeric value 29.
It can be verified that for any other combination of alphabets with the given numeric value, βaaazyβ is the lexicographically smallest.
Test Case 2: The lexicographic smallest string of length 3 and numeric value 42 is βaozβ.
It can be verified that for any other combination of alphabets with the given numeric value, βaozβ is the lexicographically smallest.
2
1 6
2 2
f
aa
For lexicographically shortest string, try to place as many βaβ in starting and βzβ in the end.
The basic idea is that we will try to place characters with maximum numeric value at the end of the string for the lexicographically smallest string. That means we want as many aβs at the start of the string and zβs at the end of the sting.
So we will first initialize a string of length βNβ with all the characters βaβ and decrement βVβ by βNβ. Now placing β z β at any position will increment the numeric value of the string by 25. So we will traverse the string from the end and place βzβ at positions (i.e. increment character value by 25 ) and decrement βVβ by 25 until the given string value is not obtained. If at any point V < 25 that means placing βzβ will exceed the given string value so Increment the character value with exactly βVβ.
Algorithm
O( N ), where βNβ is the length of the string formed.
We are forming a string of length βNβ in a single traversal. So the time complexity is O( N ).
O( N ), where βNβ is the length of the string formed.
We are using a resultant string of length βNβ, making space complexity O( N ).