[Algorithms] Delete the element of String

[Algorithms] Delete the element of String

·

1 min read

0단계 : 글자 지우기

문자열 my_string과 정수 배열 indices가 주어질 때, my_string에서 indices의 원소에 해당하는 인덱스의 글자를 지우고 이어 붙인 문자열을 return 하는 solution 함수를 작성해 주세요.

Example

my_stringindicesresult
"apporoograpemmemprs"[1, 16, 6, 15, 0, 10, 11, 3]"programmers"

Solution

function solution(my_string, indices) {
   return [...my_string].map((c,i) => indices.includes(i)?0:c).filter(n => n!=0).join('');
}
  • [...my_string] - The length of my_string is not fixed

  • map((c, i), ~~) - return the new Array. c is the current element of my_string. i is the index of my_string.

  • include -> includes

  • indices.includes(i)? 0: c) - If the element of indices includes i(==index of my_string), put 0. If not, put c(==the current element of my_string).

  • filter(n => n!=0) - Delete the element 0 from my_string