1
class Solution {
2
public:
3
string orderlyQueue(string S, int K) {
4
// for k>1 we can make it fully sorted string after roation because here we
5
// are not bound to the roatate first char only.
6
if (K > 1) {
7
sort(S.begin(), S.end());
8
return S;
9
}
10
// for k==1 we can rotate whole string any times like- S="cba" we can get
11
// cba, bac,acb so in S+S ="cbacba" we need to find only lexicographically
12
// smallest string of size n in S+S.
13
string tempr = S;
14
S = S + S;
15
for (int i = 1; i < tempr.size(); i++) {
16
tempr = min(tempr, S.substr(i, tempr.size()));
17
}
18
return tempr;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0