<p>Given string num representing a non-negative integer <code>num</code>, and an integer <code>k</code>, return <em>the smallest possible integer after removing</em> <code>k</code> <em>digits from</em> <code>num</code>.</p>

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

<pre>
<strong>Input:</strong> num = &quot;1432219&quot;, k = 3
<strong>Output:</strong> &quot;1219&quot;
<strong>Explanation:</strong> Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;10200&quot;, k = 1
<strong>Output:</strong> &quot;200&quot;
<strong>Explanation:</strong> Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;10&quot;, k = 2
<strong>Output:</strong> &quot;0&quot;
<strong>Explanation:</strong> Remove all the digits from the number and it is left with nothing which is 0.
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= num.length &lt;= 10<sup>5</sup></code></li>
	<li><code>num</code> consists of only digits.</li>
	<li><code>num</code> does not have any leading zeros except for the zero itself.</li>
</ul>

class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        # first handle zero
        while k > 0:
            findZero = False
            for i in range(len(num)):
                # print(i, num[i])
                if num[i] == "0":
                    if k >= i:
                        findZero = True
                        k -= i
                        while i < len(num) and num[i] == "0": i += 1
                        num = num[i:len(num)]
                        #print(num)
                        break
                    else: break
            if not findZero: break

        print("num after remove 0, ", num, k)

        if len(num) == 0: return "0"
        if k == 0 or num == "0": return num

        markDecrease = 0
        hasDecrease = False
        while k > 0:
            hasDecrease = False
            for i in range(markDecrease, len(num) - 1):
                if num[i] > num[i + 1]:
                    k -= 1
                    hasDecrease = True
                    markDecrease = max(i - 1, 0)
                    num = num[0:i] + num[i+1:len(num)]
                    #print("after decrease", num)
                    break
            if not hasDecrease: break

        if k == 0 or num == "0": return num

        while k > 0 and len(num) >= 1:
            for i in range(9, 0, -1):
                pos = num.find(str(i))
                #print(i, str(i), pos)
                if pos != -1:
                    k -= 1
                    num = num[0:pos] + num[pos + 1: len(num)]
                    #print("remove largesr", num)
                    break
        if len(num) == 0: return "0"
        return num

        # print("after", num)

        # for i in range(k):
        #     st, end = 100000000, -1
        #     hasZero = False
        #     for j in range(len(num)):
        #         print(j, num[j])
        #         if num[j] == '0':
        #             print("haszero")
        #             hasZero = True
        #             while j < len(num) and num[j] == '0':
        #                 st = min(st, j)
        #                 end = max(end, j)
        #                 j += 1
        #         if hasZero:
        #             k -= 1
        #             num = num[0:st] + num[end + 1:len(num)]
        #             print(k, num)
        #             break
        #     if not hasZero: break
        
17300