public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num string字符串
* @param k int整型
* @return string字符串
*/
public String removeKnums (String num, int k) {
int sum=0;
Stack<Integer> st=new Stack<>();
Stack<Integer> st1=new Stack<>();
for(int i=0;i<num.length();i++) {
int tmp=num.charAt(i)-'0';
while(!st.isEmpty() && st.peek()>tmp && sum<k) {
st.pop();
sum++;
}
st.add(tmp);
}
while(sum<k && !st.isEmpty()) {
sum++;
st.pop();
}
StringBuilder ans=new StringBuilder();
while(!st.isEmpty())
st1.add(st.pop());
while(!st1.isEmpty() && st1.peek()==0)
st1.pop();
while(!st1.isEmpty())
ans.append(st1.pop());
if(ans.length()==0) ans.append("0");
return ans.toString();
}
}