[LeetCode] Backspace String Compare 退格字符串比较
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?
这道题给了我们两个字符串,里面可能会有井号符#,这个表示退格符,键盘上的退格键我们应该都很熟悉吧,当字打错了的时候,肯定要点退格键来删除的。当然也可以连续点好几下退格键,这样就可以连续删除了,在例子2和3中,也确实能看到连续的井号符。题目搞懂了之后,就开始解题吧,博主最先想的方法就是对S和T串分别处理完退格操作后再进行比较,那么就可以使用一个子函数来进行字符串的退格处理,在子函数中,我们新建一个结果 res 的空串,然后遍历输入字符串,当遇到退格符的时候,判断若结果 res 不为空,则将最后一个字母去掉;若遇到的是字母,则直接加入结果 res 中即可。这样S和T串同时处理完了之后,再进行比较即可,参见代码如下:
解法一:
class Solution {
public:
bool backspaceCompare(string S, string T) {
return helper(S) == helper(T);
}
string helper(string str) {
string res = "";
for (char c : str) {
if (c == '#') {
if (!res.empty()) res.pop_back();
} else {
res.push_back(c);
}
}
return res;
}
};
我们也可以不使用单独的子函数,而是直接用 for 循环来处理S和T串,当然原理都是一样的,分别建立s和t的空串,然后进行退格操作,最后比较s和t串是否相等即可,参见代码如下:
解法二:
class Solution {
public:
bool backspaceCompare(string S, string T) {
string s = "", t = "";
for (char c : S) c == '#' ? s.size() > ? s.pop_back() : void() : s.push_back(c);
for (char c : T) c == '#' ? t.size() > ? t.pop_back() : void() : t.push_back(c);
return s == t;
}
};
这道题的 follow up 让我们使用常数级的空间复杂度,就是说不能新建空的字符串来保存处理之后的结果,那么只能在遍历的过程中同时进行比较,只能使用双指针同时遍历S和T串了。我们采用从后往前遍历,因为退格是要删除前面的字符,所以倒序遍历要好一些。用变量i和j分别指向S和T串的最后一个字符的位置,然后还需要两个变量 cnt1 和 cnt2 来分别记录S和T串遍历过程中连续出现的井号的个数,因为在连续井号后,要连续删除前面的字母,如何知道当前的字母是否是需要删除,就要知道当前还没处理的退格符的个数。好,现在进行 while 循环,条件是i和j至少有一个要大于等于0,然后对S串进行另一个 while 循环,条件是当i大于等于0,且当前字符是井号,或者 cnt1 大于0,若当前字符是退格符,则 cnt1 自增1,否则 cnt1 自减1,然后i自减1,这样就相当于跳过了当前的字符,不用进行比较。对T串也是做同样的 while 循环处理。之后若i和j有一个小于0了,那么可以根据i和j是否相等的情况进行返回。否则再看若S和T串当前的字母不相等,则返回 false,因为当前位置的退格符已经处理完了,剩下的字母是需要比较相等的,若不相等就可以直接返回 false 了。最后当外层的 while 循环退出后,返回i和j是否相等,参见代码如下:
解法三:
class Solution {
public:
bool backspaceCompare(string S, string T) {
int i = (int)S.size() - , j = (int)T.size() - , cnt1 = , cnt2 = ;
while (i >= || j >= ) {
while (i >= && (S[i] == '#' || cnt1 > )) S[i--] == '#' ? ++cnt1 : --cnt1;
while (j >= && (T[j] == '#' || cnt2 > )) T[j--] == '#' ? ++cnt2 : --cnt2;
if (i < || j < ) return i == j;
if (S[i--] != T[j--]) return false;
}
return i == j;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/844
参考资料:
https://leetcode.com/problems/backspace-string-compare/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Backspace String Compare 退格字符串比较的更多相关文章
- [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:比较含退格字符串【844】
LeetCode:比较含退格字符串[844] 题目描述 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = ...
- LeetCode - 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 ...
- C#LeetCode刷题之#844-比较含退格的字符串(Backspace String Compare)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...
- [Swift]LeetCode844. 比较含退格的字符串 | 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比较含退格的字符串
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- LeetCode算法题-Backspace String Compare(Java实现)
这是悦乐书的第327次更新,第350篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第197题(顺位题号是844).给定两个字符串S和T,如果两个字符串都输入到空文本编辑器 ...
随机推荐
- gojs常用API-画布定义
持续更新中 基础画布定义API画布初始位置 initialContentAlignment: go.Spot.Center,画布位置,定义后就不能拖动画布了,画布位置交由gojs管理 contentA ...
- day17——其他内置函数
zip函数: print(list(zip(('a','b','c'),(1,2,3)))) p={'name':'alex','age':18,'gender':'none'} print(list ...
- 【medium】990. Satisfiability of Equality Equations 并查集
Given an array equations of strings that represent relationships between variables, each string equa ...
- Springboot2新特性概述
官方说明: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes 起码 JDK 8 和支持 ...
- 烧写uboot和openwrt固件ARxx系列
以AR9331为例. 1.用烧录器将uboot烧写到flash中 (AR9331_U-Boot_Oolite-v1-v20170713.bin) 2.登录:192.168.1.1网页烧写uboot ...
- C#应用编程小例子-02-窗体最大化和最小化窗体大小
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- percona-toolkit安装
https://www.percona.com/downloads/percona-toolkit/LATEST/ #下载wget https://www.percona.com/downloads/ ...
- windows安装node和yarn
Ubuntu子系统安装和删除yarn 在 Debian 或 Ubuntu 上,需要用 Debian 包仓库来安装 Yarn. 首先需要配置仓库: curl -sS https://dl.yarnpkg ...
- cocos creator中粒子效果的使用
就如同上图的星星特效一样,在触碰时产生特效,但是并不销毁节点,因为要使用很多次,因此使用节点池NodePool保存起来的. 以下是使用粒子效果使要使用到的一些基本控制函数: 我的使用:
- HeadFirst学习笔记-1. 设计模式入门
1.概念 在开始学习前,我们先了解一些概念,方便我们接下来的学习. OO基础 抽象 继承 多态 封装 OO原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 设计模式 设计模式(Desi ...