class Stack {
constructor() {
this
.items = [];
}
push(element) {
return
this
.items.push(element);
}
pop() {
if
(
this
.items.length > 0) {
return
this
.items.pop();
}
}
top() {
return
this
.items[
this
.items.length - 1];
}
isEmpty() {
return
this
.items.length == 0;
}
size() {
return
this
.items.length;
}
clear() {
this
.items = [];
}
}
function
test(expression, index) {
var
i;
if
(expression[index] !=
"["
) {
console.log(expression +
", "
+ index +
": -1"
);
return
;
}
let st =
new
Stack();
for
(i = index; i < expression.length; i++) {
if
(expression[i] ==
"["
) st.push(expression[i]);
else
if
(expression[i] ==
"]"
) {
st.pop();
if
(st.isEmpty()) {
console.log(expression +
", "
+ index +
": "
+ i);
return
;
}
}
}
console.log(expression +
", "
+ index +
": -1"
);
}
test(
"[ABC[23]][89]"
, 0);
test(
"[ABC[23]][89]"
, 4);
test(
"[ABC[23]][89]"
, 9);
test(
"[ABC[23]][89]"
, 1);