再來一題
這題超簡單
而且還是hard
可以建立自信
趕緊的
@rainkaras
https://i.imgur.com/KA4byZq.png
899. Orderly Queue
You are given a string s and an integer k. You can choose one of the first k let
ters of s and append it at the end of the string.
Return the lexicographically smallest string you could have after applying the m
entioned step any number of moves.
翻譯:
每次都可以把前k個字以內的字
丟到字串後面
問你最小的字串是什麼
最小的 = lexicographically smallest
(把他當數字進位)
思路:
如果k==1的話
就只會有s.size()種字串
直接試試看就好
如果k>=2的話
每次都可以一直換換換
換到想要的位子
然後把那兩個字母前後順序交換
也就代表
只要次數多 沒有換不出來的字串
所以直接紀錄就好了
媽的
建立自信題
謝謝出題員
```cpp
class Solution {
public:
string orderlyQueue(string s, int k)
{
int n = s.size();
string res = s;
if(k == 1)
{
string now = s;
for(int i = 0 ; i < n ; i ++)
{
now.push_back(now[0]);
now = now.substr(1);
if(res > now) res = now;
}
return res;
}
int paper[26] = {};
for(char k : s)
{
paper[k-'a'] ++;
}
string res2;
for(int i = 0 ; i < 26 ; i ++)
{
for(int j = 0 ; j < paper[i] ; j ++)
{
res2.push_back(i+'a');
}
}
return res2;
}
};
```