Make Unreal REAL.
article thumbnail
Level 2. 괄호 회전하기

 

 

rotate() 함수로 하나씩 밀면서 확인하면 된다.

 

#include <iostream>
#include <algorithm>
#include <stack>

using namespace std;

int solution(string s)
{
    int answer = 0;
    size_t len = s.length();

    auto check = [](string& s) -> bool
    {
        stack<char> stck;

        for (char c : s)
        {
            if (stck.empty() || c == '(' || c == '{' || c == '[')
                stck.emplace(c);
            else if ((stck.top() == '(' && c == ')') || (stck.top() == '{' && c == '}') || (stck.top() == '[' && c == ']'))
                stck.pop();

            if (!stck.empty())
                if (stck.top() == ')' || stck.top() == '}' || stck.top() == ']')
                    return false;
        }

        return stck.empty();
    };

    while (--len)
    {
        answer += check(s);

        rotate(s.begin(), s.begin() + 1, s.end());
    }

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 1. 달리기 경주  (0) 2023.04.17
Level 2. [3차] 파일명 정렬  (0) 2023.04.16
Level 2. 가장 큰 수  (0) 2023.04.14
Level 2. 카펫  (0) 2023.04.13
Level 2. 2개 이하로 다른 비트  (0) 2023.04.12
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그