Stack solution using JavaScript array methods push and pop. O(n).
const checkEquivalentKeypresses = array => { const array1 = array[0].split(","); const array2 = array[1].split(","); const firstString = []; const secondString = []; array1.forEach(char => { if (char === "-B") { firstString.pop(); } else { firstString.push(char); } }); array2.forEach(char => { if (char === "-B") { secondString.pop(); } else { secondString.push(char); } }); return firstString.join() === secondString.join(); };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Stack solution using JavaScript array methods push and pop. O(n).