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. VMware安装步骤既常见问题

    一.vmware出问题? 可以使用vmvare的修复功能. 二.创建虚拟机 1)第一步:选择自定义下一步,典型里是都设定好了的. 2)第二步:选择12默认下一步 3)第三步:可以从光驱中安装,可以从文 ...

  2. 【题解】Luogu P5071 [Ynoi2015]此时此刻的光辉

    众所周知lxl是个毒瘤,Ynoi道道都是神仙题,题面好评 原题传送门 一看这题没有修改操作就知道这是莫队题(我也只会莫队) 我博客里对莫队的简单介绍 一个数N可以分解成\(p_1^{c_1}p_2^{ ...

  3. 逐步构建循环神经网络 RNN

    rnn.utils.py import numpy as np def softmax(x): e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axi ...

  4. 通过修改DNS达到不FQ也能访问Google(2018-12-25至现在已失效)

    一.前言 不知道各位小伙伴们现在用的搜索引擎是用Google搜索还是百度搜索呢?但我个人还是比较极力推荐用Google搜索的,首先用百度搜索后的结果的前几项大部分是满屏的广告,甚至搜索的结果并不能直接 ...

  5. Ubuntu 14.04 下安装 OpenCV

    参考: Installation in Linux Error compiling OpenCV, fatal error: stdlib.h: No such file or directory 图 ...

  6. 配置SAP GUI FOR HTML(通过WEB方式登录)

    配置SAP GUI FOR HTML(通过WEB方式登录) SAP系统可以通过安装 SAP GUI.SAP GUI FOR JAVA.SAP GUI WEB FOR JAVA.SAP GUI FOR ...

  7. 剧透 & 报名 | 蚂蚁金服ATEC城市峰会·上海即将开幕

    小蚂蚁说: 2019年1月4日,蚂蚁金服ATEC城市峰会将以“数字金融新原力(The New Force of Digital Finance)”为主题,在中国上海举办.蚂蚁金服ATEC(Ant Te ...

  8. [IDE - Eclipse] JSP报错:The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    是因为Eclipse的Web项目不自动引入相关jar包. Right Click on the Project ❯ Properties ❯ Project Facets. You would be ...

  9. [Hibernate] official tutorial - userguide

    Persistence contexts org.hibernate.Session API and javax.persistence.EntityManager API represent a c ...

  10. ios10兼容问题

    var lastTouchEnd = 0; document.addEventListener('touchend', function(event) { var now = (new Date()) ...