0
点赞
收藏
分享

微信扫一扫

leetcode 844. 比较含退格的字符串

M4Y 2022-04-23 阅读 54
leetcode

在这里插入图片描述

方法一. 重构

class Solution {
public:
    string build(string s) {
        string ans;
        for (char ch : s) {
            if (ch != '#') {
                ans.push_back(ch);
            }
            else if (!ans.empty())
                ans.pop_back();
        }
        return ans;
    }

    bool backspaceCompare(string s, string t) {
        string s_build = build(s);
        string t_build = build(t);
        if (s_build == t_build)
            return true;
        else
            return false;
    }
};

时间复杂度:O(n+m)
空间复杂度:O(n+m)。

方法二. 双指针

举报

相关推荐

0 条评论