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

<p>Return <em>the lexicographically smallest string you could have after applying the mentioned step any number of moves</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cba&quot;, k = 1
<strong>Output:</strong> &quot;acb&quot;
<strong>Explanation:</strong> 
In the first move, we move the 1<sup>st</sup> character &#39;c&#39; to the end, obtaining the string &quot;bac&quot;.
In the second move, we move the 1<sup>st</sup> character &#39;b&#39; to the end, obtaining the final result &quot;acb&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;baaca&quot;, k = 3
<strong>Output:</strong> &quot;aaabc&quot;
<strong>Explanation:</strong> 
In the first move, we move the 1<sup>st</sup> character &#39;b&#39; to the end, obtaining the string &quot;aacab&quot;.
In the second move, we move the 3<sup>rd</sup> character &#39;c&#39; to the end, obtaining the final result &quot;aaabc&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    string orderlyQueue(string s, int k) {
       map<int,int>m;
       string temp=s;
       sort(temp.begin(),temp.end());
       if(k>1) return temp;
       string ans="";
       char flag=temp[k-1];
       for(int i=0; i<k-1; i++){
           m[temp[i]-'a']++;
           ans+=temp[i];
       }
       string rest="";
       int count=k-1;
       int i;
       for(i=0; i<s.length() && count; i++){
        if(m[s[i]-'a']>0){
            m[s[i]-'a']--;
            count--;
        }
        else rest+=s[i];
       } 
       string front="";
       for(int j=i; j<s.length(); j++){
        front+=s[j];
       }
       front+=rest;
       string check="";
       string test="";
       for(int i=0; i<front.length(); i++){
        if(front[i]==flag){
            string temp2=test;
            string substr1=front.substr(i,front.length()-i);
            substr1+=temp2;
            if(check=="") check=substr1;
            else if(check>substr1) check=substr1;
        }
        test+=front[i];
       }
       ans+=check;
       return ans;
    }
};
8600