Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

Example 1:

Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".

Example 2:

Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".

Example 3:

Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".

Example 4:

Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".

Note:

  1. 1 <= S.length <= 200
  2. 1 <= T.length <= 200
  3. S and T only contain lowercase letters and '#' characters.

想法:使用两个栈,分别处理字符串S和字符串T。对于字符串S而言,逐个遍历该字符串,如果字符不为#,入栈,如果为#,则执行栈的pop()操作。对字符串T也执行同样的操作。最后再比较两个栈中元素是否相等即可

public:
    bool backspaceCompare(string S, string T) {
        std::stack<char> s;
        std::stack<char> t;
        for(char i : S){
            if(i != '#'){
                s.push(i);
            }else if(!s.empty() && i == '#'){
                s.pop();
            }
        }
        for(char j : T){
            if(j != '#'){
                t.push(j);
            }else if(!t.empty() && j == '#'){
                t.pop();
            }
        }
        return s == t;
    }
};

leetcode-844 Backspace String Compare的更多相关文章

  1. [LeetCode] 844. Backspace String Compare 退格字符串比较

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  2. 【Leetcode_easy】844. Backspace String Compare

    problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...

  3. 【LeetCode】844. Backspace String Compare 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...

  4. [LeetCode&Python] Problem 844. Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  5. 844. Backspace String Compare判断删除后的结果是否相等

    [抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...

  6. 844. Backspace String Compare

    class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...

  7. [LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  8. [LeetCode] Backspace String Compare 退格字符串比较

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  9. LeetCode算法题-Backspace String Compare(Java实现)

    这是悦乐书的第327次更新,第350篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第197题(顺位题号是844).给定两个字符串S和T,如果两个字符串都输入到空文本编辑器 ...

  10. LeetCode - Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

随机推荐

  1. 4.Factory Pattern(工厂模式)

    工厂模式(Factory Pattern)定义: 定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. 针对实现编程,但是当我们每次使用new时候,不正是在针对 ...

  2. Mybatis插件开发

    前面几篇文章介绍了Mybtis中四个重要的对象,其中提到它们都是在Configuration中被创建的,我们一起看一下创建四大对象的方法,代码如下所示: public ParameterHandler ...

  3. C++ 的那些坑 (Day 2)

    虚函数调用的例外 我们知道在通过基类的指针或者引用调用某个对象的函数时,如果这个对象是一个派生类而且该方法是一个虚方法那么一般情况下就会调用派生类的虚方法实现.这个过程是C++的多态.然而这之中有些例 ...

  4. 关于shortcut icon和icon

    语句一:<link rel="shortcut icon" href="favicon.ico" /> 语句二<link rel=" ...

  5. python内置函数每日一学 -- any()

    any(iterable) 官方文档解释: Return True if any element of the iterable is true. If the iterable is empty, ...

  6. python-命令模式

    源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 命令在发送方被激活,而在接收方被响应.一个对象既可以作为命令的发送方,也可以作为 ...

  7. 微信公众号本地断点调试(frp反向代理或Remote Debugger)

    问题描述: 需要开发微信授权和订阅推送,但是感觉调试不方便,就试着几种方式.因为是用的C#开发,Visual Studio工具自带配套的远程工具 (Remote Debugger).但是感觉不稳定,容 ...

  8. vue-router重定向 不刷新问题

    前阵子太忙了,自己一个人一边开发着新项目,一边维护着旧项目,没时间写博客,终于让我腾出时间了.废话少说,开始正文. 问题描述: 之前项目是angular开发的,后来用vue重构后.项目路径和vue路径 ...

  9. js-对象深度克隆方法

    学习收藏. 1.来自http://www.cnblogs.com/yxz-turing/p/4784861.html function cloneObj(obj){ var str, newobj = ...

  10. 数据库查询字段为null 时,返回0

    oracle select nvl(字段名,0) from 表名; sqlserver select isnull(字段名,0) from 表名; mysql select ifnull(字段名,0) ...