DEV Community

Discussion on: A coding interview question asked at Google

cbarrick profile image
Chris Barrick • Edited on

Just compare starting from the end of the strings. When you see "-B" (possibly consecutively), you know you can skip stuff. O(n) time and O(1) space.

The fact that backspace is two characters and the keypresses are separated by commas is gross. It would be so much nicer (and realistic) for the input be an ASCII string with literal backspace characters.

gabgab2003 profile image

Important thing when just skipping:
lets say we have "a,c,-B,-B" and we start from the back: we see a -B, so we skip the next character, now there is a c and then an a so we get ca (reversed), which is wrong. You need to count the -B s that are back to back and then remove as many "chars". also agree, real chars would be far better, also returning literal "true" and "false" hurts a lot