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.

Follow up:

  • Can you solve it in O(N) time and O(1) space?

class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
Sr=[]
i=0
while i<len(S):
if S[i]=='#':
if Sr:
Sr.pop()
else:
Sr.append(S[i])
i+=1 Tr=[]
i=0
while i<len(T):
if T[i]=='#':
if Tr:
Tr.pop()
else:
Tr.append(T[i])
i+=1 return Tr==Sr

  

[LeetCode&Python] Problem 844. Backspace String Compare的更多相关文章

  1. 【Leetcode_easy】844. Backspace String Compare

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

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

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

  3. [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 ...

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

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

  5. 844. Backspace String Compare

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

  6. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  7. [LeetCode&Python] Problem 796. Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  8. [LeetCode&Python] Problem 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  9. [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. centos7救援模式--单机模式(单用户模式)

    前序 经典问题: 单机模式, 当系统忘记某用户登录密码的时候可以解决密码重置问题 操作 1 开机的时候出现选择进入系统的界面时, 按E键 2 选择定位到ro这里, 替换成rw init=/sysroo ...

  2. 剑指offer(37)数字在排序数组中出现的次数。

    题目描述 统计一个数字在排序数组中出现的次数. 题目分析 这题用暴力解也可以过,不过面试官肯定期待更好的解法. 查找我们最熟悉的就是二分查找了,不过二分查找查找的数在数组中只有一个,我们这里却有很多个 ...

  3. WebApi返回的Json去掉XML

    在global.asax.cs文件中的 application_start()方法中加入下面一句话 GlobalConfiguration.Configuration.Formatters.XmlFo ...

  4. Ubuntu如何自定义tftp服务根目录

    答:修改/etc/default/tftpd-hpa中的TFTP_DIRECTORY即可,默认TFTP_DIRECTORY="/var/lib/tftpboot"

  5. Spring boot+mybatis+thymeleaf 实现登录注册,增删改查

    本文重在实现理解,过滤器,业务,逻辑需求,样式请无视.. 项目结构如下 1.idea新建Spring boot项目,在pom中加上thymeleaf和mybatis支持.pom.xml代码如下 < ...

  6. c# 7.1 Async Main方法

    安装 .net framework sdk 7.1 新建一个 .net framework 7.1 的程序 在程序的工程文件的第一个 PropertyGroup  节点下加入以下子属性   <L ...

  7. Pandas 基础(10) - 用 Pivot table 做格式转换

    Pivot allows you to transform or reshape data.Pivot 可以帮助我们改变数据的格式, 下面两个例子可以作为参考: 下面来看下具体实现, 首先引入一个 c ...

  8. 在input中输入需要的数据,使用qrcode,点击生成二维码

    话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...

  9. Series.str——字符串批量处理方法

    针对dataframe中的某一行(或列)想做批量字符串处理时,可采用此方法 series.str.python内置的str方法 例如: series.str.replace('A','B')   # ...

  10. Token国内地铁使用城市

    天津 广州 深圳 南京 武汉 台北 高雄