Skip to content

올바른 괄호 - #85

Open
yangyeeeun wants to merge 1 commit into
mainfrom
yee/correct_parenthesis
Open

올바른 괄호#85
yangyeeeun wants to merge 1 commit into
mainfrom
yee/correct_parenthesis

Conversation

@yangyeeeun

Copy link
Copy Markdown
Collaborator

🍪 문제 이름

Resolve: 올바른 괄호


🍊 문제 정의

input

  • s : String - '(' 와 ')' 로만 이루어진 문자열

output

  • boolean - 올바른 괄호이면 true, 아니면 false

[입출력 예시]

  1. s = "()()" -> answer = true
  2. s = "(())()" -> answer = true
  3. s = ")()(" -> answer = false
  4. s = "(()(" -> answer = false

🍑 알고리즘 설계

풀이 과정에서 본인이 생각한 내용을 작성해 주세요.
Deque를 사용해서 '('이면 push하고 ')'이면 pop해서 비교하고 삭제하도록 했습니다.
'('와 ')'는 map에 key와 value로 미리 저장해두었습니다.

# 특히 리뷰받고 싶은 코드 일부를 여기에 작성해 주세요.
import java.util.*;
class Solution {
    boolean solution(String s) {
        boolean answer = true;
        Deque<Character> parenthesis = new ArrayDeque<>();
        Map<Character,Character> pair = Map.of(')','(');
        for(char c : s.toCharArray()){
            if(c=='('){
                parenthesis.push(c);
            }else{
                if(parenthesis.isEmpty() || pair.get(c)!=parenthesis.pop()){
                    return false;
                }
            }
        }
        return parenthesis.isEmpty();
    }
}

🥝 최악 수행 시간 복잡도

  • O()

🍰 특이 사항 (Optional)

참고 자료나 추가로 설명하고 싶은 내용을 작성해 주세요.
굳이 map을 사용하지 않고 문자 비교를 하는 방법이 더 간단하다는 생각이 들었습니다.

@yangyeeeun yangyeeeun linked an issue Jul 14, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PGS] 올바른 괄호 / level2

1 participant