프로그래머스/Lv.1

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

MINU.SHINNNN 2023. 2. 28. 16:55

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

 

프로그래머스

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

programmers.co.kr

풀이

skip에 있는 문자는 건너뛰면서, index만큼의 갯수를 건너뛴 문자로 현재 문자를 교체해야 한다. 이때 'z'를 넘어가면 'a'부터 시작한다.

해싱을 사용해 skip문자를 기록해두고, s의 각 문자가 해시에 없다면 카운트 하는 방식으로 구현을 하였다.

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

using namespace std;

string solution(string s, string skip, int index) {
    string answer = "";
    unordered_map<int, int> um;
    // skip 문자 해싱
    for (auto& i:skip) {
        um[i-'a']=1;
    }
    
    for (auto& i:s){
        int cnt=0;
        int n=i-'a';
        while (cnt<index){
            n++;
            // z 넘어가면 a로 전환
            if (n > 'z'-'a') n=0;
            // skip에 없으면 카운트
            if (!um[n]) cnt++;
        }
        char c=n+'a';
        answer+=c;
    }
    return answer;
}