作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/backspace-string-compare/description/

题目描述

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?

题目大意

在一个空白的编辑器里连续输入两段字符,其中#代表退格,要求最后两段字符是否相同。

有个Follow up,问我们能不能使用O(n)的时间复杂度和O(1)的空间复杂度。

解题方法

字符串切片

字符串题对于Python而言都不算题。就是按照题目要求做一遍就好了。

遇到#,字符串不为空,就删除最后一个字符。如果不是#号,就拼接到字符串的最后。把两个字符串都求出来,然后比较就好。

注意,我不小心踏进了一个坑,因为看到两个连续的if,就把它们合并在一起了,其实不行的:

if s == '#':
if ans_S:
ans_S = ans_S[:-1]

我给改成了:

if s == '#' and ans_S:
ans_S = ans_S[:-1]

这样看着好看了,其实是错的。因为如果字符串是空的,那么输入#号,会把这个#号拼接到字符串上去。

Follow up的要求暂时不会。

代码如下:

class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
ans_S = ""
ans_T = ""
for s in S:
if s == '#':
if ans_S:
ans_S = ans_S[:-1]
else:
ans_S += s
for t in T:
if t == '#':
if ans_T:
ans_T = ans_T[:-1]
else:
ans_T += t
return ans_S == ans_T

使用一个栈的话,可以完美处理这个问题,遇到#退栈就好了,唯一需要注意的时候如果栈是空的时候,不能退栈。

class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
stackS, stackT = [], []
for s in S:
if s != "#":
stackS.append(s)
elif stackS:
stackS.pop()
for t in T:
if t != "#":
stackT.append(t)
elif stackT:
stackT.pop()
return stackS == stackT

日期

2018 年 6 月 10 日 —— 等了两天的腾讯比赛复赛B的数据集,结果人家在复赛刚开始就给了。。

【LeetCode】844. Backspace String Compare 解题报告(Python)的更多相关文章

  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&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 ...

  4. 【LeetCode】443. String Compression 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...

  5. LeetCode 942 DI String Match 解题报告

    题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...

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

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

  7. 844. Backspace String Compare

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

  8. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

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

随机推荐

  1. 汽车C2M模式综述

  2. OAuth2.0实战!使用JWT令牌认证!

    大家好,我是不才陈某~ 这是<Spring Security 进阶>的第3篇文章,往期文章如下: 实战!Spring Boot Security+JWT前后端分离架构登录认证! 妹子始终没 ...

  3. Spark(三)【RDD中的自定义排序】

    在RDD中默认的算子sortBy,sortByKey只能真的值类型数据升序或者降序 现需要对自定义对象进行自定义排序. 一组Person对象 /** * Person 样例类 * @param nam ...

  4. 剑指 Offer 10- I. 斐波那契数列

    写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N)).斐波那契数列的定义如下: F(0) = 0,   F(1) = 1F(N) = F(N - 1) + F(N ...

  5. 3.5 Rust Generic Types, Traits, and Lifetimes

    Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...

  6. 【Linux】【Services】【SaaS】 kubeadm安装kubernetes

    1. 简介 2. 环境 2.1. OS:  CentOS Linux release 7.5.1804 (Core) 2.2. Ansible: 2.6.2-1.el7 2.3. docker: 2. ...

  7. Nested Classes in C++

    A nested class is a class which is declared in another enclosing class. A nested class is a member a ...

  8. tableView和tableViewCell的背景颜色问题

    当在tableView中添加cell数据时,我们会发现原本设置的tableView的背景颜色不见了,这是因为加载cell数据时,tableView的背景颜色被cell数据遮盖住了,此时,可以通过设置c ...

  9. C++ friend详解

    私有成员只能在类的成员函数内部访问,如果想在别处访问对象的私有成员,只能通过类提供的接口(成员函数)间接地进行.这固然能够带来数据隐藏的好处,利于将来程序的扩充,但也会增加程序书写的麻烦. C++ 是 ...

  10. springmvc框架找那个@responseBody注解

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...