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