프로그래머스 문제풀이/LEVEL 1

[C++] 프로그래머스 문제풀이 LEVEL 1 옹알이 (2)

코딩준우 2023. 7. 3. 00:53

 

 

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

 

프로그래머스

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

programmers.co.kr

 

#include <bits/stdc++.h>


bool check(std::string & e, std::vector<std::string> & pronun){
    for (int i = 0; i < 4; ++i){
        std::string target = pronun[i];
        int len = target.length();
        int index = e.find(target, 0);
        while(index != -1){
            e.replace(index, len, std::to_string(i));
            index = e.find(target, index);
        }
    }

    int len = e.length();
    
    if (len == 1 && e[0] >= '0' && e[0] <= '9')
        return true;
    for (int i = 0; i < len - 1; ++i) {
        if (e[i] == e[i + 1] || e[i] >= 'a' && e[i] <= 'z')
            return false;
    }
    if (e[len - 1] >= 'a' && e[len - 1] <= 'z') return false;
    
    return true;
}

int solution(std::vector<std::string> babbling)
{
    int answer = 0;
    std::vector<std::string> pronun = {"aya", "ye", "woo", "ma"};
    for (auto e : babbling){
        if (check(e, pronun)) ++answer;
    }
    return answer;
}