[LeetCode] Orderly Queue

899. Orderly Queue

You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string..

Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
string orderlyQueue(string s, int k) {
if(k == 1) {
string res = s;
for(int i = 0; i < s.length(); i++)
res = min(res, s.substr(i) + s.substr(0,i));
return res;
} else {
sort(begin(s),end(s));
return s;
}
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/06/PS/LeetCode/orderly-queue/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.