프로그래머스 120956 옹알이 (1)

1 개요[ | ]

프로그래머스 120956 옹알이 (1)

2 같이 보기[ | ]

3 C++[ | ]

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

void str_replace_once(string& s, string const& search, string const& replace) {
    size_t pos = s.find(search);
    if (pos != string::npos) {
        s.replace(pos, search.length(), replace);
    }
}

bool possible(string message) {
    str_replace_once(message, "aya", "_");
    str_replace_once(message, "ye", "_");
    str_replace_once(message, "woo", "_");
    str_replace_once(message, "ma", "_");
    for(char& ch: message) {
        if(ch != '_') return false;
    }
    return true;
}

int solution(vector<string> babbling) {
    int answer = 0;
    for(string& message: babbling) {
        if(possible(message)) answer++;
    }
    return answer;
}
#include <string>
#include <vector>
using namespace std;

void str_replace_once(string& s, string const& search, string const& replace) {
    size_t pos = s.find(search);
    if (pos != string::npos) {
        s.replace(pos, search.length(), replace);
    }
}

void str_replace_all(string& s, string const& search, string const& replace) {
    string buf;
    size_t pos = 0;
    size_t prevPos;
    buf.reserve(s.size());
    while (true) {
        prevPos = pos;
        pos = s.find(search, pos);
        if (pos == string::npos) {
            break;
        }
        buf.append(s, prevPos, pos - prevPos);
        buf += replace;
        pos += search.size();
    }
    buf.append(s, prevPos, s.size() - prevPos);
    s.swap(buf);
}

bool possible(string message) {
    str_replace_once(message, "aya", "_");
    str_replace_once(message, "ye", "_");
    str_replace_once(message, "woo", "_");
    str_replace_once(message, "ma", "_");
    str_replace_all(message, "_", "");
    return message.length() == 0;
}

int solution(vector<string> babbling) {
    int answer = 0;
    for(string& message: babbling) {
        if(possible(message)) answer++;
    }
    return answer;
}