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. java基础进阶二:HashMap实现原理分析

    HashMap实现原理分析 1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二 ...

  2. js中的json的小例子

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. Async Await异步调用WebApi

    先铺垫一些基础知识 在 .net 4.5中出现了 Async Await关键字,配合之前版本的Task 来使得开发异步程序更为简单易控.   在使用它们之前 我们先关心下 为什么要使用它们.好比 一个 ...

  4. POJ2398(KB13-B 计算几何)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5968   Accepted: 3573 Descr ...

  5. maven插件 按配置加载不同环境配置文件进行打包(maven-war-plugin)

    1.配置多种不同环境 如(本地local,开发dev,测试test 环境等) <profiles> <profile> <id>local</id> & ...

  6. CSS3和HTML5新增特性及使用(保留方便查看)

    CSS3 1.边框图片 border-image: url(test.png) 10/10px;  outline:10px solid #ff0;outline-offset:15px;边框的边框, ...

  7. 2017年5月24日 HTML 基础知识(二)

    1 快捷方式:html:xt +tab   过渡XHTML html:xs+tab  严格XHTML !+tab  html5的标签结构 2.Charset   编码 <meta charset ...

  8. 前端开发笔记(2)css基础(上)

    CSS介绍 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS ...

  9. 关于ajax 传递的参数

    ajax 发送的数据,默认都是字符串,不能直接传递list(列表),或者dict(字典). 若要 传递list(列表),或者dict(字典),需要进行一些操作. list 需要进行列表序列化,在aja ...

  10. ajax分页查询

    (1)先写个显示数据的页面,分页查询需要那几个部分呢? 1.首先是查询的文本框输入,还有查询按钮,那么就开始写代码吧 1 2 3 4 <div> <input type=" ...