Broken Back Space

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 "acb#e##c" ==> "ac" "adfc##d######" ==> "" "#######" ==> "" "" ==> ""
 avatar
unknown
csharp
4 years ago
608 B
9
Indexable
string BrokenBackSpace(string s) {
    Stack<char> outStack = new Stack<char>();
    string output = "";
    for(int i = 0; i < s.Length; i++)
    {
        if (s[i] != '#')
        {
            outStack.Push(s[i]);
        }
        else
        {
            if (outStack.Count > 0)
                outStack.Pop();
        }
    }
    
    while(outStack.Count > 0)
        output += outStack.Pop();
    for (int i = 0; i < output.Length; i++)
        outStack.Push(output[i]);
    output = "";
    while(outStack.Count > 0)
        output += outStack.Pop();
    return output;
}
Editor is loading...