본문 바로가기

알고리즘 문제

LeetCode(Java) 문제 Valid Parentheses

문제 이름 :

Valid Parentheses

 

문제 설명:

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.

모든 닫힌 괄호에는 같은 유형의 열린 괄호가 대응됩니다.

 

입출력 예시:

예시 1:

입력: s = "()"

출력 : true

예시 2:

입력: s = "()[]{}"

출력 : true

예시 3:

입력: s = "(]"

출력 : false

예시 4:

입력: s = "([])"

출력 : true

 

코드:

class Solution {
    public boolean isValid(String s) {

        while(true){
            if(s.contains("()")){
                s = s.replace("()","");
            }else if(s.contains("{}")){
                s = s.replace("{}","");
            }else if(s.contains("[]")){
                s = s.replace("[]","");
            }else {
                return s.isEmpty();
            }
        }
        
    }
}

'알고리즘 문제' 카테고리의 다른 글

LeetCode (Java) 문제 Single Number  (0) 2024.10.04
LeetCode (Java) 문제 10.02  (3) 2024.10.02
LeetCode (Java) 문제  (0) 2024.09.30
백준 알고리즘 문제 (Java) 09.20  (0) 2024.09.20
오늘 알고리즘 문제 (09.10)  (0) 2024.09.10