Dhoni is the security administrator of the company you are working and you are junior to him. Dhoni’s job is to come up with new and innovative encryption algorithms to encrypt the data while sending it to the receiver.
He came up with a new encryption algorithm as follows: Let’s suppose there is a string ‘STR’ of length ‘N’ which consists of lowercase English letters and numbers from ‘0’ to ‘9’.
The above algorithm will first find the total of all the digits occurring in the string ‘STR’ and remove them from the string, then will sort the whole string in alphabetical order and push all digits sum at the end of the above-generated string. In case there is no digit present in the given string, we don't add anything at the end.
He is analyzing how effective this algorithm will be so he asked you to automate this process so that he gets the results quicker.
So can you help hip to automate this process?.
NOTE: The sum of numbers occurring in the string may have more than one digit.
EXAMPLE :Input: ‘N’ = 7, ‘STR’ = ac2bew3
Output: abcew5
In this case, the digit occurring in the ‘STR’ is ‘2’ and ‘3’. Their sum is ‘5’. And when we sort the letters in alphabetical order we get “abcew”. Hence the concatenation of both is “abcew5”.
The first line will contain the integer 'T', the number of test cases.
Each test case consists of two lines.
The first line of input contains one integer, ‘N’ the length of the string ‘STR’.
Followed by a line containing the string ‘STR’ of length ‘N’.
Output format :
For each test case, print the final string obtained after performing the mentioned steps.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
‘STR’ consists of lowercase letters or digits.
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
2
4
x3a8
5
36zab
ax11
abz9
For the first test case, the digits in the given string are ‘3’ and ‘8’. Their sum is ‘11’. And when we sort the letters in alphabetical order we get “ax”. Hence the concatenation of both is “ax11”.
Hence, the output will be: ax11
For the second test case, the digits in the given string are ‘3’ and ‘6’. Their sum is ‘9’. And when we sort the letters in alphabetical order we get “abz”. Hence the concatenation of both is “abz9”.
Hence, the output will be: abz9
2
5
78345
5
zycba
27
abcyz
Can we divide the problem into two parts?.
The idea for this approach is to divide the whole process into two parts. The first part will involve doing the sum of all the digits present in the string ‘STR’. The second part will involve the ordering of letters in alphabetical order. And then we concatenate the results of both parts and get the final result.
To solve the first part we will iterate on the given string and whenever we find a digit we add it to a variable sum. And then we create a string of that number.
For the second part, we will first, create a new string that will only contain all the letters in the given string ‘STR’. Then we will sort the letters in alphabetical order.
We can sort the letters in alphabetical order using any O(N*log(N)) algorithm for better time complexity.
Then we merge the results of both parts and return them.
Algorithm:
// The function will return true if the given character is a number.
Int isDigit(X)
// The function will return the final string after performing given operations.
String stringSum(N, STR)
O(N*(log(N))), Where ‘N’ is the input integer.
As we are sorting the string formed by the letters given in the string ‘STR’ in alphabetical order using an O(N*(log(N))) algorithm, the time complexity will be O(N*(log(N)))
O(N), Where ‘N’ is the input integer.
As we are using the extra space to create the new string formed by the letters ‘charStr’ and sum separately and then combining them which in the worst case can have a length of ‘N’, the space complexity will be O(N).