[LeetCode] 844. Backspace String Compare

0

LeetCode

목록 보기
36/58
post-thumbnail
post-custom-banner

[LeetCode] 844. Backspace String Compare

풀이

#include <stack>

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        stack<char> sStack;
        for(int i = 0; i<s.length(); ++i){
            //not '#'
            if(s[i] != '#') 
                sStack.push(s[i]);
            //'#'
            else if(!sStack.empty()) sStack.pop();
        }
        stack<char> tStack;
        for(int i = 0; i<t.length(); ++i){
            //not '#'
            if(t[i] != '#') 
                tStack.push(t[i]);
            //'#'
            else if(!tStack.empty()) tStack.pop();
        }

        if(sStack.size() != tStack.size()) return false;
        while(!sStack.empty()){
            if(sStack.top() != tStack.top()) return false;
            sStack.pop();
            tStack.pop();
        }
        return true;
    }
};

profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글