Skip to content

박영호_Valid Parentheses#48

Open
hexdrinker wants to merge 1 commit intomainfrom
hexdrinker/problem-4
Open

박영호_Valid Parentheses#48
hexdrinker wants to merge 1 commit intomainfrom
hexdrinker/problem-4

Conversation

@hexdrinker
Copy link
Copy Markdown
Contributor

🧑‍💻 언어 및 제출 결과

  • 사용 언어: JavaScript
  • 통과 여부: ✅

🧠 풀이 설명

var isValid = function(s) {
    const stack = [];
    if (s.length === 1) return false
    
    for (let i = 0; i < s.length; i++) {
        if (s[i] === "(" || s[i] === "[" || s[i] === "{") {
            stack.push(s[i])
            continue;
        }
        if (s[i] === ")" && stack.at(-1) === "(") {
            stack.pop();
            continue;
        }
        if (s[i] === "]" && stack.at(-1) === "[") {
            stack.pop();
            continue;
        }
        if (s[i] === "}" && stack.at(-1) === "{") {
            stack.pop();
            continue;
        }
        return false
    }
    if (stack.length !== 0) return false
    return true
};
  • 여는 괄호는 스택에 넣는다
  • 닫는 괄호를 만나면 스택에서 꺼내서 잘 맞는지 비교한다
  • 스택이 모두 비워지는 경우엔 true 아니면 false

📊 시간/공간 복잡도

✅ 어떠한 근거로 시간/공간 복잡도가 이렇게 나왔는지 설명해주세요.

⚡️ 풀이의 속도와 메모리 등을 캡쳐해서 올려주세요.

  • 시간 복잡도: O(n)
    • 문자열 길이 n 만큼 순회하므로 O(n)
  • 공간 복잡도: O(1)
    • 모든 문자가 여는 괄호인 경우에 최대 n/2 요소를 저장하므로 O(n)
스크린샷 2025-08-06 오후 8 18 42 스크린샷 2025-08-06 오후 8 18 54

📝 추가 설명 (선택)

  • 고민했던 포인트가 있다면 간단히 적어주세요.

🙋‍♂️ 리뷰어에게

  • 리뷰어가 보면 좋을 포인트, 질문, 궁금한 점 등을 작성해 주세요.

@github-actions github-actions bot added the TeamC Team label for TeamC label Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

TeamC Team label for TeamC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant