C++:: 프로그래머스 <둘만의 암호>

jahlee·2023년 2월 28일
0

프로그래머스_Lv.1

목록 보기
3/75
post-thumbnail

index만큼 숫자를 밀때 skip에 포함되어 있는 문자이면 index로 생각하지 않는 문제이다.
예시로 s = "a", skip = "b", index = 3 이면 결과값이 'e' 가 된다. 중간에 변화한 문자('b')가 skip에 한번 포함되기 때문

#include <string>
#include <vector>

using namespace std;

bool is_skip(char c, string skip) // 스킵문자인지
{
    for(int i=0;i<skip.size();i++)
        if (c == skip[i]) return true;
    return false;
}

string solution(string s, string skip, int index)
{
    string answer = "";
    for(int i=0;i<s.size();i++)
    {
        char c = s[i];
        for(int j=0;j<index;)
        {
            c += 1;
            if (c == 'z' + 1) c = 'a'; // 'z'를 넘어가면 'a'로 바꿔준다.
            if(!is_skip(c, skip)) // skip에 포함되지 않는 문자이면 카운트
                j++;
        }
        answer.push_back(c);
    }
    return answer;
}

0개의 댓글