<p>You are given a string <code>num</code> representing <strong>the digits</strong> of a very large integer and an integer <code>k</code>. You are allowed to swap any two adjacent digits of the integer <strong>at most</strong> <code>k</code> times.</p>

<p>Return <em>the minimum integer you can obtain also as a string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/q4_1.jpg" style="width: 500px; height: 40px;" />
<pre>
<strong>Input:</strong> num = &quot;4321&quot;, k = 4
<strong>Output:</strong> &quot;1342&quot;
<strong>Explanation:</strong> The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;100&quot;, k = 1
<strong>Output:</strong> &quot;010&quot;
<strong>Explanation:</strong> It&#39;s ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;36789&quot;, k = 1000
<strong>Output:</strong> &quot;36789&quot;
<strong>Explanation:</strong> We can keep the number without any swaps.
</pre>

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

<ul>
	<li><code>1 &lt;= num.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>num</code> consists of only <strong>digits</strong> and does not contain <strong>leading zeros</strong>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    string minInteger(string num, int k) {
        vector<int> nums(num.size(), 0);
        for (int i = 0; i < num.size(); i++) {
            nums[i] = num[i] - '0';
        }

        int n = num.size();
        for (int i = 0; i < n; i++) {
            int smallest = nums[i];
            int index = -1;
            for (int j = i + 1; j < min(i + k + 1, n); j++) {
                if (nums[j] < smallest) {
                    smallest = nums[j];
                    index = j;
                }
            }

            if (index != -1) {
                k -= (index - i); 
                // 4 - 3 + 0;
                // 1 - 2 + 1
                // cout<<"k, index, and i are: ["<<k<<", "<<index<<", "<<i<<"]\n";
                insert_and_remove(nums, i, index);
                // num = num.substr(0, i) + '0' + smallest + num.substr(i + 1, index);
                //1

            }

            if (k == 0) 
                break;
        }
        return ans(nums);
    }

    string ans(vector<int>& nums) {
        string ans = "";
        for (int i = 0; i < nums.size(); i++)
            ans += nums[i] + '0';

        return ans;
    }

    void insert_and_remove(vector<int>& nums, int i, int index) {
        int val = nums[index];
        nums.erase(nums.begin() + index);
        nums.insert(nums.begin() + i, val);
    }
    
};
/*
4321

4312
4132
1432
1342


*/
11600