LeetCode #844 Backspace String Compare
var backspaceCompare = function(s, t) {
s = s.split("");
t = t.split("");
function removeBackspace(arr) {
let index = arr.indexOf("#");
if(arr[index-1]) {
arr.splice(index-1, 2);
} else {
arr.splice(index, 1);
}
return arr.includes("#") ? removeBackspace(arr) : arr;
}
if(s.includes("#")) removeBackspace(s);
if(t.includes("#")) removeBackspace(t);
return s.join("") === t.join("");
};
// 다른 분의 풀이
/**
* @param {string} s
* @return {string}
*/
const process = s => {
const res = [];
for (x of s.split('')) {
x === '#' ? res.pop() : res.push(x);
}
return res.join('')
}
/**
* @param {string} S
* @param {string} T
* @return {boolean}
*/
var backspaceCompare = (S, T) => process(S) == process(T);