[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. # 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 <= S.length <= 2001 <= T.length <= 200SandTonly contain lowercase letters and'#'characters.
Follow up:
- Can you solve it in
O(N)time andO(1)space?
建一个helper function, 然后用stack, 如果为'#', 就stack.pop(), 否则append(c), 最后返回"".join(stack). T: O(m+n) S: O(m+n)
**imporve, 可以用two pointers 去实现 T: O(m+n) S: O(1)
Code
class Solution:
def backspaceStringCompare(self, S, T):
def helper(s):
stack = []
for c in s:
if c == '#' and stack:
stack.pop()
elif c != '#':
stack.append(c)
return "".join(stack)
return helper(S) == helper(T)
[LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers的更多相关文章
- [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 ...
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- [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 ...
- 844. Backspace String Compare判断删除后的结果是否相等
[抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...
- 844. Backspace String Compare
class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...
- C#LeetCode刷题之#844-比较含退格的字符串(Backspace String Compare)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...
- 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 ...
- leetcode844 Backspace String Compare
""" Given two strings S and T, return if they are equal when both are typed into empt ...
随机推荐
- 2333: [SCOI2011]棘手的操作[离线线段树]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2325 Solved: 909[Submit][Stat ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十一:PS/2模块⑤ — 扩展鼠标
实验十一:PS/2模块⑤ — 扩展鼠标 当普通鼠标即三键鼠标再也无法满足需求的时候,扩展鼠标即滚轮鼠标就诞生了,然而实验十一的实验目的就是实现滚轮鼠标的驱动.不过,进入整体之前,先让我们来了解一下鼠标 ...
- Visual C++ 2010项目在Visual Studio 2013中打开.rc文件提示"undefined keyword or key name: SS_REALSIZECONTROL"解决方法
1.以方式打开.rc文件. 2.删除其中包含SS_REALSIZECONTROL定义的内容. 3.在资源编辑器中打开.rc文件,重新设置Real Size Control的属性(不能在代码编辑器里重新 ...
- !important:element.style 覆盖样式问题
问题: 浏览器F12看到是这个样子. 但是我设置的样式是这样子. #iframe_close { width:750px; } 无论怎么设置样式,都无法覆盖掉element.style的样式,widt ...
- [工具] 知网(CNKI)文献下载工具
https://github.com/amyhaber/cnki-downloader 用于免费搜索,下载CNKI上的各类文献资料
- Listview多tab上滑悬浮
extends:http://blog.163.com/xueshanhaizi@126/blog/static/37250245201410541721892/ 1:近期要做一个含有两个tab切换页 ...
- [转]Android Activity的加载模式和onActivityResult方法之间的冲突
前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...
- 记我的第一个python爬虫
捣鼓了两天,终于完成了一个小小的爬虫代码.现在才发现,曾经以为那么厉害的爬虫,在自己手里实现的时候,也不过如此.但是心里还是很高兴的. 其实一开始我是看的慕课上面的爬虫教学视屏,对着视屏的代码一行行的 ...
- return 通过文件后缀名得到的函数字符串
<?php//图片处理工具类class Image{//属性private $thumb_width; //缩略图的宽private $thumb_height;//错误属性public $th ...
- HOJ-2662Pieces Assignment(状态压缩,动态规划)
Pieces Assignment Source : zhouguyue Time limit : 1 sec Memory limit : 64 M Submitted : 415, Accepte ...