-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNestingDepth.java
executable file
·64 lines (54 loc) · 2.01 KB
/
NestingDepth.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
55
56
57
58
59
60
61
62
63
64
import java.util.*;
public class NestingDepth{
private static String nestingString(String s) {
Stack<Character> stack = new Stack<>();
int previous = 0;
// Scan each digit
// Case 1: if current digit is less than previous push closing braces as many diff times
// Case 2: if current digit is greater than prebious push opening braces as many diff times
// Case 3: if current digit is equal to previous do nothing
for (int i = 0; i < s.length(); ++i) {
int current = s.charAt(i) - '0';
if (previous > current) {
// Case 1
int diff = Math.abs(previous - current);
while (diff > 0) {
stack.push(')');
diff--;
}
} else if (previous < current) {
// Case 2
int diff = Math.abs(previous - current);
while (diff > 0) {
stack.push('(');
diff--;
}
}
// Push current digit
stack.push(s.charAt(i));
previous = current;
}
// String is empty, push closing braces for last digit
while (previous > 0) {
stack.push(')');
previous--;
}
// Empty the stack contents in string buffer
StringBuffer sb = new StringBuffer();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
// Reverse the string buffer content since stack poped out in reverse order
String result = sb.reverse().toString();
return result;
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; ++i) {
String s = sc.next();
System.out.print("Case #" + i + ": " + nestingString(s));
System.out.println("");
}
}
}