[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 ...
随机推荐
- 【cs229-Lecture12】K-means算法
上课内容: 无监督学习: K-means聚类算法 混合高斯模型 jensen不等式(用于推导出EM算法的一般形式) EM(Expectation Maximization)算法(最大期望算法) K-m ...
- IT零起步-CentOS6.4部署OpenVPN服务器
OpenVPN是一个用于创建虚拟专用网络加密通道的软件包,实现二/三层的基于隧道的VPN.最早由James Yonan编写.OpenVPN允许创建的VPN使用公开密钥.数字证书.或者用户名/密码来进行 ...
- 【Python系列】Python3获取控制台输入
""" 接收控制台的输入 How old are you? 18 How tall are you ? 180 How much do you weigh? 50 So ...
- svn版本管理
代码发布方案: 1,安装,优化 软件环境,(nginx,lvs) <-------运维工程师 2,程序代码(不断更新). <--------开发工程师,(开发,运维都可以发布) 3, ...
- 基于Python的跨平台端口转发工具
背景 使用lcx也好,nc也好,总是会被安全防护软件查杀,所以想着自己写一个.顺面学习一下,端口转发的原理. 端口转发的逻辑 端口转发的逻辑很简单开启两个scoket,一个绑定IP端口进行listen ...
- Docker 容器管理:rancher
Rancher:https://www.cnrancher.com/ 是一个开源的企业级全栈化容器部署及管理平台. 定位上和 K8s 比较接近,都是通过 web 界面赋予完全的 docker 服务编排 ...
- Linux大文件已删除,使用df查看已使用的空间并未减少
今天在一台Linux系统里,使用df命令查看磁盘使用情况,发现/根分区使用了100% 然后删除一大文件/var/lib/pgsql/9.1/pgstartup.log,再使用df查看使用情况,显示内容 ...
- [ZT] matlab中plot画图参数的设置
一.Matlab绘图中用到的直线属性包括: (1)LineStyle:线形 (2)LineWidth:线宽 (3)Color:颜色 (4)MarkerType:标记点的形状 (5)MarkerSize ...
- Mapreduce 原理及程序分析
1.MapReduce(Map+Reduce) 提出一个问题: 目标:你想数出一摞牌中有多少张黑桃. 直观方式:一张一张检查并且数出有多少张是黑桃数目 MapReduce方法则是: 给在座的所有玩家中 ...
- POJ 1556 - The Doors - [平面几何+建图spfa最短路]
题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...