-
Notifications
You must be signed in to change notification settings - Fork 1
/
10th_december.java
54 lines (45 loc) · 1.32 KB
/
10th_december.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
String str = read.readLine();
Solution ob = new Solution();
System.out.println(ob.buildLowestNumber(str,N));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution {
static String buildLowestNumber(String str, int k) {
// code here
Stack<Character> st=new Stack<>();
for(char c:str.toCharArray()){
while(!st.isEmpty() && st.peek()>c && k>0){
st.pop();
k--;
}
st.push(c);
}
while(!st.isEmpty() && k>0){
st.pop();
k--;
}
StringBuilder sb=new StringBuilder();
while(!st.isEmpty()){
sb.append(st.pop());
}
while(sb.length()>0 && sb.charAt(sb.length()-1)=='0'){
sb.setLength(sb.length()-1);
}
if(sb.length()==0)return "0";
return sb.reverse().toString();
}
}