코테 좀 해라★彡/C++

올바른 괄호

요미 ★ 2024. 3. 28. 01:49

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
bool solution(const char* s) {
    //기본 true 값
    bool answer = true;

    //count값
    int countA = 0;
    int countB = 0;
    
    //문자열을 비교하고 카운트 한다
    for(int i = 0; i < strlen(s); i++)
    {
        if(s[i]=='(') 
            countA++;
        else 
            countB++;
        
        //')'가 애초에 많으면 false 임
        if(countB > countA) 
        {
            answer = false;
            break;
        }
       
    }
    
    if(answer && countA == countB)
        answer = true;
    else
        answer = false;
    
    return answer;
}