문제 설명
두 문자열 s와 skip, 그리고 자연수 index가 주어질 때, 다음 규칙에 따라 문자열을 만들려 합니다. 암호의 규칙은 다음과 같습니다.
- 문자열 s의 각 알파벳을 index만큼 뒤의 알파벳으로 바꿔줍니다.
- index만큼의 뒤의 알파벳이 z를 넘어갈 경우 다시 a로 돌아갑니다.
- skip에 있는 알파벳은 제외하고 건너뜁니다.
예를 들어 s = "aukks", skip = "wbqd", index = 5일 때, a에서 5만큼 뒤에 있는 알파벳은 f지만 [b, c, d, e, f]에서 'b'와 'd'는 skip에 포함되므로 세지 않습니다. 따라서 'b', 'd'를 제외하고 'a'에서 5만큼 뒤에 있는 알파벳은 [c, e, f, g, h] 순서에 의해 'h'가 됩니다. 나머지 "ukks" 또한 위 규칙대로 바꾸면 "appy"가 되며 결과는 "happy"가 됩니다.
두 문자열 s와 skip, 그리고 자연수 index가 매개변수로 주어질 때 위 규칙대로 s를 변환한 결과를 return하도록 solution 함수를 완성해주세요.
코드 작성
#include <string>
#include <vector>
#include <set>
#include <iostream>
using namespace std;
string solution(string s, string skip, int index) {
string answer = "";
// skip에 포함된 문자들을 set으로 저장 (빠른 검색을 위해)
set<char> skipSet(skip.begin(), skip.end());
for (char ch : s) {
char current = ch;
// index 만큼 이동하며 skip 처리
for (int i = 0; i < index; ) {
current++; // 다음 문자로 이동
if (current > 'z') { // z를 넘어가면 a로 순환
current = 'a';
}
if (skipSet.find(current) == skipSet.end()) {
i++;
}
}
answer += current;
}
return answer;
}'Algorithm > Practice' 카테고리의 다른 글
| [Algorithm] 성격 유형 검사하기 (0) | 2025.01.15 |
|---|---|
| [Algorithm] 햄버거 만들기 (2) | 2025.01.14 |
| [Algorithm] 대충 만든 자판 (0) | 2025.01.13 |
| [Algorithm] 문자열 나누기 (0) | 2025.01.10 |
| [Algorithm] 체육복 (1) | 2025.01.10 |