Why is string.erase() not working in a specific case? [C++]

Question: 844. Backspace String Compare

My code:

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        int m = s.size(), n = t.size();
        
        if (m != n) return false;
        
        int backspaces = 0;
        
        for (int i = 0; i < m; i++) {
            cout << s << " " << t << endl;

            if (s[i] == '#') {
                if (i == 0) s.erase(i, 1);
                else s.erase(i-1, 2);
            }
            if (t[i] == '#') {
                if (i == 0) t.erase(i, 1);
                else t.erase(i-1, 2);
            }
        }
        
        cout << s << " " << t << endl;
        
        return s == t;
    }
};

For input:

"ab##"
"c#d#"

string.erase is not working for above inputs.


Screenshot:

image


I've read stackoverflow questoin, but I didn't make the same mistake as that question asker did.

Can someone help?

Comments (1)