favicon Jay Lee DevLog

📚 목차

오늘부터 프로그래머스에 코딩테스트를

입문부터 시간나는대로 풀어서 정리해보려 한다.

문제는 프로그래머스에 다 나와있으니 따로 올리진 않겠다.

 

#include <string>
#include <vector>
#include <regex>

using namespace std;

string ReturnResult(int nLength)
{                                
    string strResult("");
    for(int i=0; i<nLength; i++)
    {
        strResult += "1";
    }
    
    return strResult;
}

int solution(vector<string> babbling) {
    int answer = 0;
    string strMatchList[4] = {"aya", "ye", "woo", "ma"};
    
    for(auto& i : babbling)
    {
        string strTest = i;
        for(auto j : strMatchList) 
        {
            /*regex re(j);
            if (std::regex_match(strTest, re))
                answer++;*/
            
            strTest = regex_replace(strTest, regex(j), "1");
            
            if (strTest == ReturnResult(strTest.size())) 
            {
                answer++;   
                break;
            }
        }
        
    }
    return answer;
}

 

일단 나같은 경우에는 

regex를 사용해서 풀어봤고,

 

일치하는 문자를 '1'로 바꿔서(ReturnResult 함수)

모든 문자가 '1'로 바꼈으면

카운트를 하는 형식으로 풀어봤다.

 

왜 이렇게 했냐하면,

regex_replace로 일치하는 문자를 삭제하다 보니,

문자가 삭제되면서 다시 조건에 만족하는 문자가 만들어지는 경우가 있었다.

"wyeoo" 같은 경우가 해당 경우에 속했다.

ye를 지우면 woo가 되기 때문에 

그냥 바뀐건 다른 문자로 변환시켜서 해결했다.

 

문자열 관련 문제는 확실히 regex 사용하면

풀기 쉬운게 많은 것 같다.

+ Recent posts

/ /

Contact

📧 dlwjdwls60@naver.com


블로그에 내용이 있으면 해당 글을 보여주며, 없으면 내용이 복사된 채로 ChatGPT로 연결됩니다.