-
[Programmers] JavaScript 알고리즘 | Lv.0 글자 지우기► JS Algorithm/Programmers 2024. 7. 21. 21:56반응형
🔒 문제 설명
문자열 my_string과 정수 배열 indices가 주어질 때, my_string에서 indices의 원소에 해당하는 인덱스의 글자를 지우고 이어 붙인 문자열을 return 하는 solution 함수를 작성해 주세요.
🔒 제한사항
- 1 ≤ indices의 길이 < my_string의 길이 ≤ 100
- my_string은 영소문자로만 이루어져 있습니다
- 0 ≤ indices의 원소 < my_string의 길이
- indices의 원소는 모두 서로 다릅니다.
🔒 입출력 예
my_string indices result "apporoograpemmemprs" [1, 16, 6, 15, 0, 10, 11, 3] "programmers"
🔒 입출력 예 설명
입출력 예 #1
- 예제 1번의 my_string의 인덱스가 잘 보이도록 표를 만들면 다음과 같습니다.
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 my_string a p p o r o o g r a p e m m e m p r s - indices에 있는 인덱스의 글자들을 지우고 이어붙이면 "programmers"가 되므로 이를 return 합니다.
🔐 solution of mine
Array.prototype.splice()
Array.prototype.filter()
function solution(my_string, indices) { let answer = [...my_string]; for (let i of indices) { answer.splice(i, 1, ""); } return console.log(answer.filter((v) => v).join("")); } solution("apporoograpemmemprs", [1, 16, 6, 15, 0, 10, 11, 3]); //output: "programmers"
🔐 solution of others
String.prototype.includes()
function solution(m, a) { let answer = ""; for (let i in m) { if (!a.includes(+i)) { answer += m[i]; } } return console.log(answer); } solution("apporoograpemmemprs", [1, 16, 6, 15, 0, 10, 11, 3]); //output: "programmers"
🔐 solution of others
Array.prototype.filter()
Array.prototype.includes()
Array.prototype.join()
const solution = (s, d) => console.log([...s].filter((v, i) => !d.includes(i)).join("")); solution("apporoograpemmemprs", [1, 16, 6, 15, 0, 10, 11, 3]); //output: "programmers"
🔐 solution of others
Set
Array.prototype.filter()
Set.prototype.has()
Array.prototype.join()
const solution = (my_string, indices) => { const set = new Set(indices); return console.log([...my_string].filter((v, i) => !set.has(i)).join("")); };
🔐 solution of others
Array.prototype.reduce()
Array.prototype.includes()
const solution = (my_string, indices) => { let answer = ""; return console.log( [...my_string].reduce( (acc, cur, i) => (acc += indices.includes(i) ? "" : cur), "" ) ); }; solution("apporoograpemmemprs", [1, 16, 6, 15, 0, 10, 11, 3]); //output: "programmers"
반응형'► JS Algorithm > Programmers' 카테고리의 다른 글
[Programmers] JavaScript 알고리즘 | Lv.0 배열 만들기 5 (0) 2024.07.27 [Programmers] JavaScript 알고리즘 | Lv.0 문자열 뒤집기 (0) 2024.07.27 [Programmers] JavaScript 알고리즘 | Lv.0 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기 (0) 2024.07.21 [Programmers] JavaScript 알고리즘 | Lv.0 빈 배열에 추가, 삭제하기 (0) 2024.07.21 [Programmers] JavaScript 알고리즘 | Lv.0 이차원 배열 대각선 순회하기 (0) 2024.07.21