최대 1 분 소요

Question

Q


Solution

  • 배열에 찾아야할 옹알이를 담는다.
  • 현재 옹알이 temp에서 배열에 담긴 옹알이를 찾고 찾으면 지워준다.
  • 연속인지 체크해야 하기때문에 prev를 추가한다.
  • 나왔던 옹알이가 다시 나올 수 있기 때문에 찾으면 인덱스 j를 -1 줄여 준다.

Cord

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string correctBabbling[4] = { "aya", "ye", "woo", "ma" };
int babblingSize[4] = { 3, 2, 3, 2 };

int solution(vector<string> babbling)
{
    int answer = 0;

    for (int i = 0; i < babbling.size(); ++i)
    {
        string temp = babbling[i];
        string prevStr = "";

        for (int j = 0; j < 4; ++j)
        {
            // 찾으면 해당 범위의 string 제거
            auto it = temp.find(correctBabbling[j]);

            if (temp == "" || prevStr[0] == temp[0]) { break; }
            else if (temp[0] != correctBabbling[j][0]) { continue; }
            if (temp.find(correctBabbling[j]) != string::npos)
            {
                temp.erase(it, it + babblingSize[j]);
                prevStr = correctBabbling[j];
                j = -1;
            }
        }

        if (temp == "") { answer += 1; }
    }

    return answer;
}

Result

Result