Skip to content

Commit

Permalink
Update 0402-remove-k-digits.java
Browse files Browse the repository at this point in the history
  • Loading branch information
sujal-goswami authored Apr 11, 2024
1 parent b87ba1d commit 36da36e
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions java/0402-remove-k-digits.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
class Solution {
public String removeKdigits(String num, int k) {

if(k==num.length() || k>num.length()){
public String removeKdigits(String num, int k) {
if(k >= num.length())
return "0";
}

Stack<Character> st=new Stack<>();

for(int i=0;i<num.length();i++){
char ch=num.charAt(i);
while(!st.isEmpty() && k>0 && st.peek()>ch){
st.pop();
Stack<Character> stack = new Stack<>();
for(char d: num.toCharArray()){
while(!stack.empty() && d < stack.peek() && k > 0){
stack.pop();
k--;
}
st.push(ch);
stack.push(d);
}

while(!st.isEmpty() && k>0){
st.pop();
while(!stack.empty() && k > 0){
stack.pop();
k--;
}

StringBuilder sb = new StringBuilder();
while(!st.isEmpty())
sb.append(st.pop());

sb.reverse();

while(sb.length()>1 && sb.charAt(0)=='0')
sb.deleteCharAt(0);

return sb.toString();


StringBuilder res = new StringBuilder();
while(!stack.empty()){
res.insert(0, stack.pop());
}

for(int i = 0; i < res.length(); i++){
if(res.charAt(i) != '0'){
return res.toString().substring(i, res.length());
}
}
return "0";
}
}
}

0 comments on commit 36da36e

Please sign in to comment.