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######" ==> "" "#######" ==> "" "" ==> ""
mail@pastecode.io avatar
unknown
csharp
3 years ago
608 B
5
Indexable
Never
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;
}