当k=2时,我们可以互换任意2个相邻的元素,因此可以使用冒泡排序来使字符串变得有序。
string orderlyQueue(string S, int K) {
if (K == 1) {
int size = S.size();
string ans = S;
S = S+S;
for (int i = 0; i < size; ++i) {
string tmp = S.substr(i,size);
if(ans.compare(tmp)>0)
ans = tmp;
}
return ans;
}
else {
sort(S.begin(), S.end());
return S;
}
}