[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 <= 200
1 <= T.length <= 200
S
andT
only 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 ...
随机推荐
- 三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...
- 金蝶KIS问题解决汇总
1.帐套结转时,提示t_subsys插入重复键 解决: I.删除索引 alter table t_subsys drop constraint pk_subsys II.t_rp_initial表 ...
- VIM 如何使用系统的剪切板
想要将系统剪贴板里的内容复制到 vi 编辑的文档中怎么办? 例如,在网页上复制了一段文字,想贴到本地的某个文件中. 使用 vi 打开本地文件,在 输入 模式下,按 Shift + Insert 详细可 ...
- 有关xml中的xmlns
1. xmlns "xmlns"是XHTML namespace的缩写,叫做"名字空间"声明.名字空间是什么作用呢?我的理解是:由于xml允许你自己定义自己的标 ...
- Oracle相关内容整理
一.常用sql 1.查看版本 SELECT * FROM V$VERSION; SELECT version FROM V$INSTANCE 2.数据库发生死锁时,跟踪文件的位置 关于跟踪文件,大义是 ...
- 2015.7.10js-07(简单时间)
今天学习了一个小程序,将本地时间显示在页面上,用了图片的形式. 1.执行原理是,先用6张全0的图片,然后通过循环img各自根据时间来更换相对应的时间图片. 2.使用Date()函数获取本地时间,然后转 ...
- Maven:版本管理 【SNAPSHOT】【Release】【maven-release-plugin】【nexus】
什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基本用 ...
- C++ 标准输出cout与printf
C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...
- 扩展KMP算法小记
参考来自<拓展kmp算法总结>:http://blog.csdn.net/dyx404514/article/details/41831947 扩展KMP解决的问题: 定义母串S和子串T, ...
- 图论最短路——dijkstra
下午直接开始dijkstra的堆优化,很简单的这里把书上的原理说一下吧,小心和prim最小生成树的堆优化迷,Dijkstra算法基于贪心思想,它只适用于所有边都是非负数的图.当变长z都是非负数的时候, ...