Backspace

Question

Solution

Idea

Time Complexity: O(n)O(n)

This is a pretty good question regarding string manipulation. The whole idea is shown below:

The soul/genius point in this question is regarding the use of index. This variable is used to store the location we will write to in our result string. And when we encounter <, we just move our index back by 1, so next time we can overwrite or move back again. This is awesome!

This structure also gives us a new way to use for loop to iterate through a string! That is to set the terminating condition to be line[i] != 0.

Always keep in mind that the optimal way to loop through a string is to visit the index of the char array instead of changing the pointer!

Code

https://github.com/mendax1234/Coding-Problems/blob/main/kattis/backspace/backspace.c

Last updated