<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) {
       priority_queue<char,vector<char>,greater<char>>minHeap;
       priority_queue<char>maxHeap;
       if(k==s.length())
       {
        sort(s.begin(),s.end());
        return s;
       }
       for(int i=0;i<k;i++){
        maxHeap.push(s[i]);
       }
       for(int i=k;i<s.length();i++){
        minHeap.push(s[i]);
       }
       string minstring=s;
       if(k==1){
       
       for(int i=s.length()-1;i>=0;i--){
        s=s+s[0];
        s.erase(0,1);
        minstring=min(minstring,s);
       }
       return minstring;

       }
    //    cout<<"size of minheap and maxheap are; "<<minHeap.size()<<" "<<maxHeap.size();
       while(minHeap.top()<maxHeap.top()){
        char x= minHeap.top();
        char y= maxHeap.top();
        minHeap.pop();
        maxHeap.pop();
        minHeap.push(y);
        maxHeap.push(x);
       }

       string ans="";
       while(!maxHeap.empty()){
        char x= maxHeap.top();
        
        ans= maxHeap.top()+ ans;
        maxHeap.pop();
       }
       while(!minHeap.empty()){
        char x= minHeap.top();
      
        ans+=minHeap.top();
        minHeap.pop();
       }
        return ans;
    }
};
11300