프로그래머스/Lv.1
프로그래머스 JS [둘만의 암호]
sakuraop
2023. 2. 3. 21:59
둘만의 암호 문제 보기
문제 요약
- 두 문자열 s와 skip, 그리고 자연수 index가 주어질 때, 다음 규칙에 따라 문자열을 만들려 합니다.
- 문자열 s의 각 알파벳을 index만큼 뒤의 알파벳으로 바꿔줍니다.
- index만큼의 뒤의 알파벳이 z를 넘어갈 경우 다시 a로 돌아갑니다.
- skip에 있는 알파벳은 제외하고 건너뜁니다.
ex) "a", skip:"bcd", index: 5
a efgeg
bcd (스킵)
=> "g"
문제 풀이
// 스킵할 코드 배열
const skipArray = [...skip.split("").map((alphabet) => alphabet.charCodeAt())];
[ 119, 98, 113, 100 ]
// 스킵된 코드 배열
const asciiArray = new Array(26).fill(0).map((el, index) => el + 97 + index);
const skippedAsciiArray = asciiArray.filter((el) => !skipArray.includes(el));
[
97, 99, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110,
111, 112, 114, 115, 116, 117,
118, 120, 121, 122
] skippedAsciiArray
// 스킵된 배열을 알파벳으로 변환하여 충분한 길이만큼 반복
const skippedAlphabetArray = skippedAsciiArray.map((ascii) => String.fromCharCode(ascii)).join("").repeat(3);
acefghijklmnoprstuvxyzacefghijklmnoprstuvxyzacefghijklmnoprstuvxyz
// indexOf를 이용하여 인덱스만큼 이동한 문자를 합쳐서 반환
const answer = [];
s.split("").forEach((alphabet) => answer.push(skippedAlphabetArray[skippedAlphabetArray.indexOf(alphabet) + index]));
return answer.join("");
[ 'h', 'a', 'p', 'p', 'y' ]
happy