# Daily coding: 괄호 짝 맞추기
# Question
https://www.hackerrank.com/challenges/ctci-balanced-brackets/problem
3
{[()]} // YES
{[(])} // NO
{{[[(())]]}} // YES
괄호 짝이 맞으면 YES, 아니면 NO
# My Answer
function fn(str) {
Object.prototype.values = function(obj) {
return Object.keys(obj).map(function(key) { return obj[key] });
}
let pair = {']':'[', '}': '{', ')': '('};
let stack = [];
let arr = str.split('');
while(arr.length > 0) {
let item = arr.shift();
if(Object.values(pair).includes(item)) {
stack.push(item);
} else if(Object.keys(pair).includes(item)) {
if(stack.pop() !== pair[item]) {
return 'NO';
}
} else {
return 'NO';
}
}
return stack.length == 0? 'YES' : 'NO';
}
# jsjfuentesj's answer
def is_matched(expression):
pairs = {'{' : '}', '[' : ']', '(' : ')'}
sk = []
for c in expression:
if c in pairs:
sk.append(pairs[c])
elif not sk or c != sk.pop():
return False
return not sk