Skip to content

[YeomChaeeun] Week 6 #890

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

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions valid-parentheses/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* valid-parentheses
* 괄호의 열고 닫히는 짝을 확인하는 알고리즘
* Stack(LIFO) 데이터 구조 사용
* 알고리즘 복잡도
* - 시간 복잡도: O(n)
* - 공간 복잡도: O(n)
* @param s
*/
function isValid(s: string): boolean {

// 접근 1 - {}, (), [] 가 포함되는지 보고 replace문으로 단순하게 풀어봄..
// while (s.includes("{}") || s.includes("()") || s.includes("[]")) {
// s = s.replace("{}", "");
// s = s.replace("()", "");
// s = s.replace("[]", "");
// }
// return s === '';

// 접근 2 - leetCode의 hint를 보고 stack 을 적용
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const stack: string[] = []
const pairs: {[key: string]: string} = {
'}': '{',
')': '(',
']': '['
}

for (const char of s) {
if (!pairs[char]) {
// 여는 괄호 저장
stack.push(char)
} else {
// 닫는 괄호와 매칭 확인
if (stack.pop() !== pairs[char]) {
return false
}
}
}

return stack.length === 0
}

Loading