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. Ubuntu如何自定义tftp服务根目录

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

  2. CSS基础学习(二) 之 width min-width max-width属性

    width 1. 设置元素内容区(content area)的宽度. 2. 如果box-szing属性设置为border-box,width表示border area的宽度,如下图 min-width ...

  3. Springboot静态文件不更新的解决办法,以及Springboot实现热部署

    Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...

  4. Putting Boxes Together CodeForces - 1030F (带权中位数)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

  5. 『OpenCV3』霍夫变换原理及实现

    霍夫变换常用于检测直线特征,经扩展后的霍夫变换也可以检测其他简单的图像结构. 在霍夫变换中我们常用公式 ρ = x*cosθ + y*sinθ 表示直线,其中ρ是圆的半径(也可以理解为原点到直线的距离 ...

  6. js基本类型存放和对象存放的区别(对象遍历)

    js的基本类型,对象类型的应用在初学的时候,需要自己加以明确,明确了数据类型,在使用过程中才能正确使用变量.如下两个例子是摘自初学时的笔记,为大家提供参考. 1.对象可以存放属性和方法,js基本类型不 ...

  7. mac 不小心把管理员改成了普通用户

    最近手贱,不小心把mbp仅有的管理员账号设置成了普通用户,想重新改回管理员,竟然需要用其他管理员账号去更改.简直 那个 啥 ~ 下面将提供创建一个新的管理员账号的方案 不仅适用于上述我出现的问题,也适 ...

  8. linux-Centos7安装python3并与python2共存

    1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...

  9. c# Excel 行高、列宽、合并单元格、单元格边框线、冻结

    http://www.2cto.com/kf/201007/52724.html 新增选择整列: Range = ((Range)(WALeTieSheet.Cells[1, PwCStartCol_ ...

  10. ceph Luminous版手动安装零散记录

    1.安装必要的依赖包,关防火墙,向/etc/hosts内添加域名等 2.安装ceph 配置yum源 (如果嫌慢,可以配置cachedir=/home/yum/$basearch/$releasever ...