C++:: 프로그래머스 < 문자열 나누기 >

jahlee·2023년 4월 18일
0

프로그래머스_Lv.1

목록 보기
20/75
post-thumbnail

첫 문자와 다른 문자의 개수를 유의해주면서 풀면되는 간단한 문제이다.

#include <string>
#include <vector>
using namespace std;

int solution(string s)
{
    int answer = 0, idx = 0;
    while (idx < s.size())
    {
        int cnt = 1;//첫문자 - 다른문자
        char c = s[idx++];
        while (idx < s.size() && cnt)// cnt == 0 이 될때까지
        {
            if (s[idx] != c) cnt--;// 다른 문자면 
            else cnt++;// 같은 문자면
            idx++;
        }
        answer++;
    }
    return answer;
}

0개의 댓글