Study/Algorithm

프로그래머스 (C/C++) 181862 : 세 개의 구분자

hwooo 2023. 7. 5. 16:32

https://school.programmers.co.kr/learn/courses/30/lessons/181862

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


풀이

문자열에 a,b,c가 없는 경우 문자열은 잘리지 않는다. 구분자 사이에 다른 문자가 없을 경우 저장하지 않는다 해서 a,b,c 중 하나의 문자가 나와야 저장이 이루어지는 줄 알았는데 아니었다.

코드

#include <string>
#include <vector>

using namespace std;

vector<string> solution(string myStr) {
    vector<string> answer;
    string S = "";
    
    for(auto i : myStr){
        if(i == 'a' || i == 'b' || i == 'c'){
            if(!S.empty()) answer.push_back(S);
            S.clear();
        }
        else S += i;
    }
    if(!S.empty()) answer.push_back(S);
    
    if(answer.empty()) return {"EMPTY"};
    return answer;
}