알고리즘/프로그래머스 1단계

프로그래머스 - 둘만의 암호 - C++

게임만드는학생 2024. 7. 22. 14:42

https://school.programmers.co.kr/learn/courses/30/lessons/155652

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

string solution(string s, string skip, int index) {
    string answer = "";
    
    for(int i=0;i<s.length();i++)
    {
        int cnt=0;
        while(cnt<index)
        {
            s[i]++;
            if(s[i]>122)s[i]-=26;
            bool b=false;
            for(int j=0;j<skip.length();j++)
            {
                if(s[i]==skip[j])
                {
                    b=true;break;
                }
            }
            if(b)continue;
            cnt++;
                
        }
    }
    
    return s;
}

 

주어진 문자열 s에서 index만큼 알파벳을 증가시켜서 새로운 문자열을 만드는데 이 때, skip에 있는 알파벳들은 건너뛴다. 

while문을 통해서 index만큼 건너뛰는지를 cnt로 체크했다. 

s[i]를 증가시키며 skip에 있는 알파벳이 아니라면 cnt를 증가시키고 아니면 다시 반복한다. 

index만큼 증가하게 되면 while문이 종료되고 다음 글자로 넘어간다.