Make Unreal REAL.
article thumbnail
Level 0. 옹알이 (1)

 

 

정규 표현식을 이용하여 해결했다.

 

regex_replace 시에 마지막에 regex_constants::format_first_only 파라미터를 주면 첫 번째 매치만 교체할 수 있다.

 

일치된 문자열을 길이만큼 '_'로 바꾼 후 모든 문자가 '_'로 바뀌었는지 확인하였다.

 

#include <iostream>
#include <vector>
#include <regex>

using namespace std;

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

    for (string s : babbling)
    {
        size_t length = s.length();
        vector<string> words = {"aya", "ye", "woo", "ma"};

        for (string word : words)
            s = regex_replace(s, regex(word), string(word.length(), '_'), regex_constants::format_first_only);

        answer += (s == string(length, '_'));
    }

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 0. OX퀴즈  (0) 2023.01.28
Level 0. 안전지대  (0) 2023.01.27
Level 0. 등수 매기기  (0) 2023.01.25
Level 0. 평행  (1) 2023.01.24
Level 0. 최빈값 구하기  (0) 2023.01.23
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그