[Algorithm] ๋๋ง์ ์ธ์ด
๐๋ฌธ์
๋ ๋ฌธ์์ด 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 ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
- 5 ≤ s์ ๊ธธ์ด ≤ 50
- 1 ≤ skip์ ๊ธธ์ด ≤ 10
- s์ skip์ ์ํ๋ฒณ ์๋ฌธ์๋ก๋ง ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- skip์ ํฌํจ๋๋ ์ํ๋ฒณ์ s์ ํฌํจ๋์ง ์์ต๋๋ค.
- 1 ≤ index ≤ 20
์ ์ถ๋ ฅ ์
"aukks" | "wbqd" | 5 | "happy" |
โ๏ธ ํ์ด
s๊ฐ 50 ์ดํ์ด๊ณ , index๊ฐ 20 ์ดํ๊ธฐ ๋๋ฌธ์ ํ์ธํ๋ฉด์ ์งํํด๋ ๊ด์ฐฎ๊ฒ ๋ค๊ณ ์๊ฐํ๋ค.
String.fromCharCode()์ ์ฌ์ฉํ๋ค. ์ด ๋ฉ์๋๋ ์์คํค์ฝ๋ ๊ฐ์ ์ด์ฉํด ์ํ๋ฒณ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์ํ๋ฒณ์ 97๋ถํฐ 122๊น์ง ์กด์ฌํ๋ค. ๋ฐ๋ผ์ ํ์ฌ s ์์ ํ์ฌ ์ํ๋ฒณ์ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํด ์์คํค์ฝ๋๊ฐ์ ๊ตฌํ๋ค.(s.charCodeAt(i))
์ด ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฏธ๋ฆฌ skip์ ์ํ๋ฒณ์ ๋ฃ์ด๋ skipSet์์ String.fromCharCode(idx)๊ฐ์ด ์๋์ง ํ์ธํ๊ณ (์์คํค์ฝ๋๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ํ๋ฒณ) ์๋ ๊ฒฝ์ฐ cnt๋ฅผ ์ฆ๊ฐ์์ผ์ฃผ๊ณ , ์๋ ๊ฒฝ์ฐ ์ฆ๊ฐ์์ผ์ฃผ์ง ์์ผ๋ฉด์ cnt๊ฐ index๊ฐ ๋ ๋๊น์ง while๋ฌธ์ ์ด์ฉํด์ ๋ฐ๋ณตํ๋ค. cnt๊ฐ index ๋งํผ ๋์์ ๋ idx๋ฅผ ์ด์ฉํด์ ์ํ๋ฒณ์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ณ answer์ ์ถ๊ฐํด์คฌ๋ค.
(๋ฐ๋๋ก skipSet์ ์ํ๋ฒณ์ ๋ฃ์ด์ฃผ์ง ์๊ณ ์์คํค์ฝ๋ ๊ฐ์ ๋ฃ์ด์ค๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ๋ค.)
โจ๏ธ ์ฝ๋
function solution(s, skip, index) {
var answer = '';
const skipSet = new Set(skip.split(''));
for (let i = 0; i < s.length; i++) {
let cnt = 0;
let idx = s.charCodeAt(i);
while (cnt < index) {
idx = idx + 1 > 122 ? 97 : idx + 1;
if (!skipSet.has(String.fromCharCode(idx))) cnt += 1;
}
answer += String.fromCharCode(idx);
}
return answer;
}