Leetcode844.Backspace String Compare比较含退格的字符串
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。
示例 1:
输入:S = "ab#c", T = "ad#c" 输出:true 解释:S 和 T 都会变成 “ac”。
示例 2:
输入:S = "ab##", T = "c#d#" 输出:true 解释:S 和 T 都会变成 “”。
示例 3:
输入:S = "a##c", T = "#a#c" 输出:true 解释:S 和 T 都会变成 “c”。
示例 4:
输入:S = "a#c", T = "b" 输出:false 解释:S 会变成 “c”,但 T 仍然是 “b”。
提示:
- 1 <= S.length <= 200
- 1 <= T.length <= 200
- S 和 T 只含有小写字母以及字符 '#'。
class Solution {
public:
bool backspaceCompare(string S, string T) {
int len1 = S.size();
int len2 = T.size();
vector<char> str1(len1, 0);
vector<char> str2(len2, 0);
int cnt1 = 0;
int cnt2 = 0;
for(int i = 0; i < len1; i++)
{
if(S[i] == '#' && cnt1 > 0)
{
cnt1--;
continue;
}
if(S[i] != '#')
str1[cnt1++] = S[i];
}
for(int i = 0; i < len2; i++)
{
if(T[i] == '#' && cnt2 > 0)
{
cnt2--;
continue;
}
if(T[i] != '#')
str2[cnt2++] = T[i];
}
if(cnt1 != cnt2)
return false;
for(int i = 0; i < cnt1; i++)
{
if(str1[i] != str2[i])
return false;
}
return true;
}
};
Leetcode844.Backspace String Compare比较含退格的字符串的更多相关文章
- [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 ...
- C#LeetCode刷题之#844-比较含退格的字符串(Backspace String Compare)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...
- LeetCode844 比较含退格的字符串
题目描述: 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
- LeetCode--844--比较含退格的字符串(java)
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...
- leetcode-844-比较含退格的字符串(用vector取代stack)
题目描述: 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = ...
- Q844 比较含退格的字符串
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", 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 ...
随机推荐
- HZOI2019 超级树 dp
题面:https://www.cnblogs.com/Juve/articles/11207540.html(密码)————————————————>>> 题解: 官方题解: 考虑d ...
- 洛谷P3749 [六省联考2017]寿司餐厅
传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...
- vue.js_12_vue的watch和computed
1.watch用来监测指定Vue实例上的数据变动. watch主要用于监控vue实例的变化,它监控的变量当然必须在data里面声明才可以,它可以监控一个变量,也可以是一个对象. 1.>使用wat ...
- php对输入的检测
$data['value'] = trim(stripslashes(htmlspecialchars_decode($value)));
- thinkPHP使用中踩的坑,记录一下(不停更)
版本3.2.3 1.数据库操作中的连贯操作table(),在查询的时候可以切换表,但是在插入,更新的时候请不要使用.例如 D('user')->table('auth')->add($da ...
- 2019-2019-2-20175332-实验三《敏捷开发与XP实践》实验报告
一.编码标准 题目要求: 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能. 实验步骤 1.安装ali ...
- linux学习(一)-----vm、centos安装
安装vm和Centos 1)先安装 virtual machine ,vm12 2)再安装 Linux (CentOS 6.8) 3)原理示意图,这里我们画图说明一下 VM 和 CentOS 的关系. ...
- VirtualBox安装CentOS后分辨率和鼠标无缝切换问题
问题:VirtualBox安装完后出现分辨率只有800*600和1024*768,鼠标不能在虚拟机和本机件无缝切换. 解决办法:在终端中执行以下命令 yum install kernel yum in ...
- PAT甲级——A1033 To Fill or Not to Fill
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...
- 多对多关联懒加载导致failed to lazily initialize a collection of role: 实体类, could not initialize proxy - no Session 追加配置fetch = FetchType.EAGER解决
一篇文章需要关联很多个标签,所以他们呈一对多(多对多)的关系 org.springframework.web.util.NestedServletException: Request processi ...