Assume "#"
is like a backspace in string. This means that string "a#bc#d"
actually is "bd"
Your task is to process a string with "#"
symbols.
Examples
"abc#d##c" ==> "ac"
"abc##d######" ==> ""
"#######" ==> ""
"" ==> ""
(요약) #
가 backspace
일 때 최종적으로 남는 문자열을 구하라.
function cleanString(s) { while(s.includes('#')) { if(!s.indexOf('#')) { s = s.replace(/#+/, ''); } const index = s.indexOf('#'); s = s.slice(0, index - 1) + s.slice(index + 1); } return s; };
문자열에
#
이 없을 때까지 반복하는데우선 앞에
#
이 나오는건 의미가 없으니 반복문 처음에#
을 다 날려버림.그 다음
#
의 인덱스를 찾아서 그 앞 문자와#
을 하나씩 날려버림.마지막에 남은 문자열을
return
.