Link
문제
풀이
(1)
function solution(s, skip, index) {
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
const skipSet = new Set(skip);
const filtered = alphabet.filter(c => !skipSet.has(c))
const len = filtered.length;
let tmp = []
for (let i = 0; i < s.length; i++) {
const t = s.charAt(i);
tmp.push(filtered[(filtered.indexOf(t) + index) % len]);
}
return tmp.join('');
}
(2)
function solution(s, skip, index) {
const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z"].filter(c => !skip.includes(c));
return s.split("").map(c => alphabet[(alphabet.indexOf(c) + index) % alphabet.length]).join("");
}
(3)
function solution(s, skip, index) {
// 'a'부터 'z'까지의 알파벳 중 skip에 없는 문자들을 allowed 배열에 추가합니다.
const allowed = [];
for (let i = 97; i <= 122; i++) { // 97은 'a', 122는 'z'
const char = String.fromCharCode(i);
if (!skip.includes(char)) {
allowed.push(char);
}
}
let result = "";
// 문자열 s의 각 문자에 대해 변환을 수행합니다.
for (const ch of s) {
const pos = allowed.indexOf(ch); // 현재 문자의 allowed 내 위치 찾기
const newPos = (pos + index) % allowed.length; // index만큼 이동 (wrap-around 적용)
result += allowed[newPos];
}
return result;
}
'알고리즘 > 문제' 카테고리의 다른 글
[프로그래머스 | Lv. 1 | JavaScript] 완주하지 못한 선수 (1) | 2025.02.24 |
---|---|
[프로그래머스 | Lv. 1 | JavaScript] 대충 만든 자판 (1) | 2025.02.23 |
[프로그래머스 | Lv. 1 | JavaScript] 문자열 나누기 (0) | 2025.02.18 |
[프로그래머스 | Lv. 1 | JavaScript] 로또의 최고 순위와 최저 순위 (0) | 2025.02.17 |
[프로그래머스 | Lv. 1 | JavaScript] [1차] 다트 게임 (0) | 2025.02.15 |
※ 쿠팡 파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있습니다.