-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackspace.java
More file actions
32 lines (30 loc) · 1.1 KB
/
Copy pathBackspace.java
File metadata and controls
32 lines (30 loc) · 1.1 KB
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
// import java.text.DecimalFormat;
// import java.text.NumberFormat;
import java.util.Scanner;
import java.util.Stack;
public class Backspace {
public static void main(String[] args) {
// long start = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
char[] arr = sc.nextLine().toCharArray();
Stack<Character> stack = new Stack<>();
int bracketCount = 0;
// read from the right
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == '<')
bracketCount++;
else if (arr[i] != '<' && bracketCount > 0)
bracketCount--;
else
stack.push(arr[i]);
}
StringBuilder strB = new StringBuilder();
while (!stack.isEmpty()) {
strB.append(stack.pop());
}
System.out.println(strB.toString());
// long end = System.currentTimeMillis();
// NumberFormat formatter = new DecimalFormat("#0.00000");
// System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
}
}