GitHubSeob
C++ / 프로그래머스 / 둘만의 암호 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/155652
코딩테스트 연습 - 둘만의 암호
두 문자열 s와 skip, 그리고 자연수 index가 주어질 때, 다음 규칙에 따라 문자열을 만들려 합니다. 암호의 규칙은 다음과 같습니다. 문자열 s의 각 알파벳을 index만큼 뒤의 알파벳으로 바꿔줍니다. i
school.programmers.co.kr
문제풀이
alp에 알파벳을 먼저 저장해 준다. 다음 스킵을 탐색하면서, 겹치는 알파벳이 있으면 alp에서 삭제한다.
그다음 map을 이용해 index칸 뒤에 있는 알파벳을 저장한다. code['현재 알파벳'] = 'index칸 뒤 알파벳'
s를 탐색하면서 code[s[idx]]의 값을 answer에 계속 더해준다.
코드
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
string solution(string s, string skip, int index) {
int idx(0), idx2(0);
string answer("");
string alp = "abcdefghijklmnopqrstuvwxyz";
map<char, char>code;
sort(skip.begin(), skip.end());
while (!skip.empty()) {
if (alp[idx] == skip[0]) {
alp.erase(idx, 1);
skip.erase(0, 1);
}
else {
++idx;
}
}
for (idx = 0; idx < alp.size(); ++idx) {
code[alp[idx]] = alp[(idx + index) % alp.size()];
}
for (idx = 0; idx < s.size(); ++idx) {
answer += code[s[idx]];
}
return answer;
}
'Programmers > Level 1' 카테고리의 다른 글
C++ / 프로그래머스 / 문자열 나누기 (0) | 2023.06.21 |
---|---|
C++ / 프로그래머스 / 대충 만든 자판 (0) | 2023.06.21 |
C++ / 프로그래머스 / 햄버거 만들기 (0) | 2023.06.21 |
C++ / 프로그래머스 / 성격 유형 검사하기 (0) | 2023.06.21 |
C++ / 프로그래머스 / 바탕화면 정리 (0) | 2023.06.21 |