Smallest String After Ordered Operations

Easy
0/40
Average time to solve is 15m
profile
Contributed by
13 upvotes
Asked in company
Deloitte

Problem statement

You are given a string β€˜S’ of length β€˜N’ and a positive integer β€˜K’. You can perform the following operation any number of times on ’S’.

In each operation, you can delete one of the first β€˜K’ characters of β€˜S’ and append it at the end of the β€˜S’.

You have to find the lexicographically smallest string you can obtain by performing any number of operations.

Note:

String β€˜p’ is lexicographically smaller than string β€˜q’, if β€˜p’ is a prefix of β€˜q’, is not equal to β€˜q’ or there exists β€˜i’, such that β€˜p[i]’  <  ’q[i]’ and for all β€˜j’  <  ’i’ it is satisfied that β€˜p[j]’  =  ’q[j]’.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer β€˜T’ representing the number of test cases. 

The first line of each test case will contain two space-separated integers  β€˜N’ and β€˜K’ which denotes the length of β€˜S’ and integer β€˜K’. 

The second line of each test case will contain a single string β€˜S’.  
Output Format:
For each test case, return the lexicographically smallest string obtained by performing any number of operations on β€˜S’.
Note:
You don’t need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
1<= K <= N
S[ i ] belongs to {a to z}

Time limit: 1 sec
Sample Input 1:
2
5 1
acbaa
3 2
onm
Sample Output 1:
aaacb
mno
Explanation of sample input 1:
In the first test case, 
In the first move we will delete β€˜a’ from the beginning and put it at the end, β€˜S’ = β€œcbaaa”.
In the second move we will delete β€˜c’ from the beginning and put it at the end, β€˜S’ = β€œbaaac”.
In the third move we will delete β€˜b’ from the beginning and put it at the end, β€˜S’ = β€˜aaacb’.

For the second test case, 
In the first move we will delete β€˜n’ and put it at the end, β€˜S’ = β€œomn”.
In the second move we will delete β€˜o’ from the beginning and put it at the end, β€˜S’ = β€œmno”.
Sample Input 2:
2
1 1      
t
12 12
codingninjas
Sample Output 2:
t
acdgiijnnnos
Hint

Can we obtain all the possible permutations?

Approaches (1)
Sorting

For the value of the β€˜K’ > 1 it is possible to obtain any permutation of the string β€˜S’. Considering two characters at index β€˜i’ and β€˜i + 1’ we will prove that it is possible to swap them. 

 

  • We will delete β€˜i - 1’ characters from the prefix and put them at the end of the string β€˜S’.
  • Now β€˜ith’ and β€˜i + 1’   characters will be first and second characters now.
  • Now, we will delete the second character and put it at the end of the string β€˜S’.
  • Now we will delete the first character and put it at the end of the string β€˜S’.

  

After this operation, two characters are swapped.

This way by swapping any two consecutive characters we can obtain any permutation. 

 

For β€˜K’ = 1, after any number of operations, we will get one of its circular rotations. 

Eg for β€œabcd” : β€œabcd”,  β€œbcda”, β€œcdab”, β€œdabc” are possible.

 

We will compare all the circular rotations and pick the smallest of all as our answer. 

 

The steps are as follows:

 

  1. Create a string named β€˜res’ to store the final result and it is initialized to β€˜~’.
  2. If β€˜K’ > 1:
    1. β€˜res’ = all the characters of β€˜S’ in sorted order.
  3. Else
    1. To get all the circular permutations we will append one copy of β€˜S’ at the end of it and take all the substrings of β€˜S’ of length β€˜N’.
    2. Iterate from 0 to β€˜N’ - 1. (say, iterator = 'i'):
      1. β€˜res’ = min(β€˜res’,  substring(β€˜S’, i, ’N’ ))
  4. Return β€˜res’
Time Complexity

O(N ^ 2), where β€˜N’ is the length of the string β€˜S’.

 

When β€˜K’ = 1, we will compare β€˜N’ strings of length β€˜N’ to get the lowest possible string. So the overall time complexity will be O(N ^ 2).

Space Complexity

O(N), where β€˜N’the length of the string β€˜S’.

 

The space required to store the final result β€˜res’ will be O (N). So the overall space complexity will be O(N)

Code Solution
(100% EXP penalty)
Smallest String After Ordered Operations
Full screen
Console