Problem title
Difficulty
Avg time to solve

Missing Vertex In Parallelogram
Easy
10 mins
Sort a half-sorted array
Easy
10 mins
Sort Array
Moderate
15 mins
Largest Submatrix with Equal Number of 0's and 1's
Moderate
15 mins
Minimum Insertions
Moderate
20 mins
Task Scheduler
Moderate
35 mins
Mike and Mobile
Moderate
35 mins
Find Kth row of Pascal's Triangle
Easy
15 mins
Kruskal’s Minimum Spanning Tree Algorithm
Moderate
--
Longest Valid Parentheses
Moderate
10 mins
5

String Transformation

Difficulty: MEDIUM
Contributed By
Avg. time to solve
23 min

Problem Statement

Given a string (STR) of length N, you have to create a new string by performing the following operation:

Take the smallest character from the first 'K' characters of STR, remove it from STR and append it to the new string.

You have to perform this operation until STR is empty.

 Note:
The input string(STR) will not contain any spaces.

Assume that all characters in STR are lower case letters.

If characters less than 'K' remain, then append them in a sorted way to the new string.
Example:
Let the input string be "edcba" with K = 4.

Let the new string to be formed is initially empty, newString = "".
The first set of 4 characters are, ('e', 'd', 'c', 'b')
Out of these 4 characters, the smallest one is 'b' and hence we add it to the newString and it becomes, 
newString = "b"

The next set of 4 characters are, ('e', 'd', 'c', 'a')
Out of these 4 characters, the smallest one is 'a' and hence we add it to the newString and it becomes, 
newString = "ba"

Now we are left with "edc" and since we can't get a window of size 4, we sort them in the increasing order and append them to the newString.

Hence, newString thus formed will be "bacde".
Input format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first and the only line of each test case or query contains a string(STR) and an integer(K) separated by a single space.
Output Format :
For each test case, print the new string formed by applying the operations.

Output for every test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given functions.
Constraints :
1 <= t <= 100
0 <= N <= 10^5
1 <= K <= 10^5

Time Limit: 1 sec
Sample Input 1 :
2
badce 2
abe 2
Sample Output 1 :
abcde
abe
Explanation For Sample Input 1:
For the 1st test case:
The smallest character of the string is 'a', followed by 'b', 'c', 'd', and 'e' respectively. Hence the output string is "abcde".

For the 2st test case:
The smallest character of the string is 'a', followed by 'b', and 'e' respectively. Hence the output string is "abe".    
Sample Input 2 :
1
codingninjas 3
Sample Output 2 :
cdigninjanos
Reset Code
Full screen
copy-code
Console