1. JavaScript
function solution(S) {
if(S.length == 0){
return 1
}
let bracket = {
')': '(',
'}': '{',
']': '['
}
let stack = [];
let list = Array.from(S);
for(c of list){
if(!bracket.hasOwnProperty(c)){
stack.push(c);
}else if(stack.length == 0 || bracket[c] != stack.pop()){
return 0
}
}
if(stack.length == 0){
return 1
}else{
return 0
}
}
2. Python
def solution(S):
if len(S) == 0 :
return 1
stack = []
bracket = {
')': '(',
'}': '{',
']': '['
}
for char in S:
if char not in bracket:
stack.append(char)
elif not stack or bracket[char] != stack.pop():
return 0
if len(stack) == 0:
return 1
else:
return 0