
题目链接:https://leetcode.cn/problems/orderly-queue/
计算字典序最小的字符串,我们需要讨论k = 1 和 k > 1时两种情况
当k = 1时,我们每次取 i 个首字符并将其移动到字符串末尾,对比找最小的字典序字符串即可
当k > 1时,一定可以经过移动将字符串s变成字符串按照升序排序
func orderlyQueue(s string, k int) string {
if k == 1{
ans := s
for i := 1; i < len(s); i++{
s = s[1:] + s[:1]
if s < ans{
ans = s
}
}
return ans
}
ans := []byte(s)
sort.Slice(ans, func(i, j int) bool {return ans[i] < ans[j]})
return string(ans)
}