Skip to content

[clara-shin] WEEK 06 solutions #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions valid-parentheses/clara-shin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 괄호 문자열 유효성 검사
*
* 스택 자료구조 활용
* 1. 괄호 쌍을 매핑하는 객체를 생성하고 조건을 확인
* 2. 열린 괄호를 만나면 해당하는 닫힌 괄호를 스택에 직접 push
* 3. 닫는 괄호를 만났을 때, 스택이 비어있거나 짝이 맞지 않으면 false
* 4. 문자열을 모두 처리한 후, 스택이 비어있어야(문자열 길이가 0이어야) 모든 괄호가 올바르게 짝지어진 것(true)
*/

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
// 빈 문자열이나 홀수 길이는 유효하지 않음
if (s.length === 0 || s.length % 2 !== 0) return false;

const stack = [];

for (let i = 0; i < s.length; i++) {
const char = s[i];

if (char === '(') {
stack.push(')');
} else if (char === '{') {
stack.push('}');
} else if (char === '[') {
stack.push(']');
} else if (stack.length === 0 || stack.pop() !== char) {
// 닫는 괄호를 만났을 때, 스택이 비어있거나 짝이 맞지 않음
return false;
}
}

return stack.length === 0;
};