<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)
    {
            string ans;
            ans = s;
        // when n > 1 then we can have the string sorted , took the example of S = "aacb" and k = 2, we can get aabc after some rotations
        if (k > 1)
        {
            sort(s.begin(), s.end());
            return (s);
        }
        else
        {
            // when k ==1 then we have to see the lexicographically smallest string of size of s in the string s+s
            // like if we have s= "cba" and k ==1 then s+s = "cbacba" then we have the answer acb, this is because we can have only 1 rotation at a time and that too of the first element and we can sent it back only which we can find using s+s 
            // the relative order of other elements would remain the same, so we can use a string s+s to find the substring with lexicographically smalles first character
            string temp = "";
            temp = s + s;
            for (int i = 0; i + s.length() <= temp.length(); i++)
            {
                ans = min(ans, temp.substr(i, s.length()));
            }
            return ans;
        }
    }
};
10200