[프로그래머스/C++]Lv.1 - 둘만의 암호

YH J·2023년 5월 8일
0

프로그래머스

목록 보기
80/168

문제 링크

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

내 풀이

글자를 for문으로 ++해간다. 조건문으로 z를 넘어가는지와 skip에 해당하는 글자인지 체크한다. z를넘어가면 a로 돌려주고 skip에 해당하는 글자면 i를 --해준다.

내 코드

#include <string>
#include <vector>

using namespace std;

string solution(string s, string skip, int index) {
    string answer = "";   
    for(auto& word : s)
    {
        if(skip.find(word) == string::npos)
            for(int i = 0; i < index; i++)
            {
                word++;
                if(word > 'z')
                    word -= 'z'-'a' + 1;
                if(skip.find(word) != string::npos)
                    i--;
            }
    }
    return s;
}

다른 사람의 풀이

#include <string>
#include <vector>

using namespace std;

string solution(string s, string skip, int index) {
    string answer = "";

    for(auto v : s)
    {
        int t = 0;
        int c = v - 'a';
        while(t < index)
        {
            c++;
            v = (c % 26) + 'a';
            if(skip.find(v) == string::npos)
            {
                t++;
            }
        }
        answer += v;
    }
    return answer;
}

다른 사람의 풀이 해석

char를 int로 변환하여 계산하였다. s를 변환시켜 리턴하지 않고 변환값을 answer에 담아 넘겼다.

profile
게임 개발자 지망생

0개의 댓글