-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
27 lines (23 loc) · 766 Bytes
/
Copy pathsolution.java
File metadata and controls
27 lines (23 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public String removeKdig(String s, int k) {
StringBuilder st = new StringBuilder();
for (char ch : s.toCharArray()) {
while (st.length() > 0 && k > 0 && st.charAt(st.length() - 1) > ch) {
st.deleteCharAt(st.length() - 1);
k--;
}
st.append(ch);
}
// remove remaining digits
while (k > 0 && st.length() > 0) {
st.deleteCharAt(st.length() - 1);
k--;
}
// remove leading zeros
int idx = 0;
while (idx < st.length() && st.charAt(idx) == '0')
idx++;
String ans = st.substring(idx);
return ans.length() == 0 ? "0" : ans;
}
}