Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
var isValid = function(s) {
let result;
let stack=[]; // 스택 배열 생성
for(let i=0; i<s.length;i++){
if(s[i]==='('){
stack.push(')');
}else if(s[i]==='['){
stack.push(']');
}else if(s[i]==='{'){
stack.push('}');
}else if(stack.length > 0 && s[i]===stack[stack.length-1]){
stack.pop();
}else{
result=false;
break;
}
result=true;
}
if(result && stack.length) result=false;
// true로 리턴되었는데 stack이 남아있는 경우 false반환 -> ex) {
return result;
};