package leetcode;

/**
 * https://leetcode.com/problems/remove-k-digits/
 */
public class Problem402 {
    public String removeKdigits(String num, int k) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem402 prob = new Problem402();
        System.out.println(prob.removeKdigits("1432219", 3)); // 1219
        System.out.println(prob.removeKdigits("1432219", 4)); // 119
        System.out.println(prob.removeKdigits("1423219", 2)); // 12219
        System.out.println(prob.removeKdigits("10200", 1)); // 200
        System.out.println(prob.removeKdigits("10", 2)); // 0
        System.out.println(prob.removeKdigits("1234", 2)); // 12
        System.out.println(prob.removeKdigits("4321", 2)); // 21
        System.out.println(prob.removeKdigits("31299", 2)); // 129
    }
}

@@ -12,16 +12,41 @@
 tcode;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * h
@@ -186,85 +186,114 @@
     
-// TODO%0A        return null
+Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        int count = k
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args
+for (int i = 0; i %3C num.length(); i++
 ) %7B%0A
@@ -304,41 +304,59 @@
     
+  
-Problem402 prob = new Problem402(
+  Integer n = Integer.valueOf(num.charAt(i) - '0'
 );%0A 
@@ -366,535 +366,772 @@
     
+ 
-System.out.println(prob.removeKdigits(%221432219%22, 3)); // 1219%0A        System.out.println(prob.removeKdigits(%221432219%22, 4)); // 119%0A        System.out.println(prob.removeKdigits(%221423219%22, 2)); // 12219%0A        System.out.println(prob.removeKdigits(%2210200%22, 1)); // 200%0A        System.out.println(prob.removeKdigits(%2210%22, 2)); // 0%0A        System.out.println(prob.removeKdigits(%221234%22, 2)); // 12%0A        System.out.println(prob.removeKdigits(%224321%22, 2)); // 21%0A        System.out.println(prob.removeKdigits(%2231299%22, 2)); // 129
+   if (!stack.isEmpty() && n %3C stack.peek() && count %3E 0) %7B%0A                while (!stack.isEmpty() && n %3C stack.peek() && count %3E 0) %7B%0A                    stack.pop();%0A                    count--;%0A                %7D%0A            %7D%0A            stack.push(n);%0A        %7D%0A        while (count %3E 0) %7B%0A            stack.pop();%0A            count--;%0A        %7D%0A        StringBuilder sb = new StringBuilder();%0A        while (!stack.isEmpty()) %7B%0A            sb.append(stack.pop());%0A        %7D%0A        String result = sb.reverse().toString();%0A        if (result.isEmpty()) %7B%0A            return %220%22;%0A        %7D%0A        // Remove leading zeros.%0A        int i = 0;%0A        while (i %3C result.length() && result.charAt(i++) == '0') %7B%0A        %7D%0A        return result.substring(i - 1);
 %0A   

Solve problem 402