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.

&nbsp;
Example 1:

Input: s = "cba", k = 1
Output: "acb"
Explanation: 
In the first move, we move the 1st character 'c' to the end, obtaining the string "bac".
In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".


Example 2:

Input: s = "baaca", k = 3
Output: "aaabc"
Explanation: 
In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab".
In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".


&nbsp;
Constraints:


	1 &lt;= k &lt;= s.length &lt;= 1000
	s consist of lowercase English letters.


class Solution:
    def orderlyQueue(self, s: str, k: int) -> str:
        if k>1:
            s=list(c for c in s)
            s.sort()
            return ''.join(s)
        s1=s
        for i in range(len(s)):
            s=s[1:]+s[0]
            s1=min(s1,s)
        return s1
// Time O(n)
// Space O(n)
class Solution {
    public String orderlyQueue(String s, int k) {
        int n = s.length();
        String ans = "";
        if (k == 1){
            s+=s; // add itself again
            for (int i = 0; i < n; i++) if (ans.isEmpty() || s.substring(i, i+n).compareTo(ans) < 0){
                ans = s.substring(i, i+n);
            }
        }else{
            char[] arr = s.toCharArray();
            Arrays.sort(arr);
            ans = String.valueOf(arr);
        }
        return ans;
    }
}
class Solution {
public:
string orderlyQueue(string s, int k) {
 
if(k>1){
       sort(s.begin(),s.end());
       return s;
    }
    else{
        string res=s;
        
        for(int i=0;i<s.length();i++){
            s=s.substr(1)+s.substr(0,1);
            res=min(res,s);
        }
        return res;
    }
    
    return s;
    
}
};
/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var orderlyQueue = function(s, k) {
  // rotate the string one by one, and check which is lexographically smaller
  if (k === 1) {
    let temp = `${s}`;
    let smallest = `${s}`;
    let count = 0;
    while (count < s.length) {
      temp = temp.substring(1, s.length) + temp.charAt(0);
      if (temp < smallest) {
        smallest = temp;
      }
      count++;
    }
    return smallest;
  }
  
  // if k is greater than 1, any permutation is possilbe
  // so we simply return the sorted string (convert to array -> sort -> back to string)
  if (k > 1) {
    return [...s].sort().join('');
  }
  
  return s;
};
Orderly Queue