-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexBracket.java
52 lines (45 loc) · 1.62 KB
/
IndexBracket.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
package Stack;
public class IndexBracket {
static void test(String expression, int index) {
int i;
// If index given is invalid and is
// not an opening bracket.
if (expression.charAt(index) != '[') {
System.out.print(expression + ", "
+ index + ": -1\n");
return;
}
// Stack to store opening brackets.
Stack<Integer> st = new Stack<>();
// Traverse through string starting from
// given index.
for (i = index; i < expression.length(); i++) {
// If current character is an
// opening bracket push it in stack.
if (expression.charAt(i) == '[') {
st.push((int) expression.charAt(i));
} // If current character is a closing
// bracket, pop from stack. If stack
// is empty, then this closing
// bracket is required bracket.
else if (expression.charAt(i) == ']') {
st.pop();
if (st.empty()) {
System.out.print(expression + ", "
+ index + ": " + i + "\n");
return;
}
}
}
// If no matching closing bracket
// is found.
System.out.print(expression + ", "
+ index + ": -1\n");
}
public static void main(String[] args) {
test("[ABC[23]][89]", 0); // should be 8
test("[ABC[23]][89]", 4); // should be 7
test("[ABC[23]][89]", 9); // should be 12
test("[ABC[23]][89]", 1); // No matching bracket
}
}