给定 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. 1 <= S.length <= 200
  2. 1 <= T.length <= 200
  3. 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比较含退格的字符串的更多相关文章

  1. [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 ...

  2. C#LeetCode刷题之#844-比较含退格的字符串​​​​​​​(Backspace String Compare)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...

  3. LeetCode844 比较含退格的字符串

    题目描述: 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = ...

  4. 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)

    目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...

  5. LeetCode--844--比较含退格的字符串(java)

    给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...

  6. leetcode-844-比较含退格的字符串(用vector取代stack)

    题目描述: 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = ...

  7. Q844 比较含退格的字符串

    给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...

  8. 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 ...

  9. leetcode844 Backspace String Compare

    """ Given two strings S and T, return if they are equal when both are typed into empt ...

随机推荐

  1. 2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...

    title author date CreateTime categories WPF shows that some windows in multithreading will be locked ...

  2. 吴恩达《机器学习》课程总结(18)_照片OCR

    18.1问题描述和流程图 (1)图像文字识别是从给定的一张图片中识别文字. (2)流程包括: 1.文字侦测 2.字符切分(现在不需要切分了) 3.字符分类 18.2滑动窗口 在行人检测中,滑动窗口是首 ...

  3. 一个班六个人,学号语文、数学、英语,接收成绩(不接受学号),输出学号成绩,总分、平均分,按总分排序(原生JS)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. pip安装requests报错unicodeEncodeError:'ascii' codec can\t encode charactesers in position 9-12:ordinal not in range(128)

    前提 : 已经安装pip(pip的安装我参考的是本博客转载脚本之家的步骤,实验可以成功) 1. 在cmd输入命令转到pip安装目录: 2. 运行后出现错误 3. 步骤2中的错误应该和编码有关.搜索百度 ...

  5. 1.开始Spring

    1 对Spring的认识 为了实现控制反转,我们可以想象java创建了一个工厂类,工厂类可以去读取配置文件 比如一个.property文件.通过这个文件中的配置,反射创建一些对象,当外界需要某个类的对 ...

  6. 【vue移动端架子】vue-h5-template

    作者大大的地址:https://github.com/sunnie1992/vue-h5-template 我们运行项目,倒是可以看一看效果 虽然就是显示的UI,但是应该可以知道作者大大想要什么东西了 ...

  7. mysql本地导入数据

    1.获得一个超级权限的用户 grant all on *.* to root@'127.0.0.1' identified by 'root';# 因为我想在本地导入数据,而数据就在本地.# 有时候, ...

  8. 字符串常用方法(转载--https://www.cnblogs.com/ABook/p/5527341.html)

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  9. java 7,8 排序异常

    排序报 java.lang.IllegalArgumentException: Comparison method violates its general contract! 要明确返回-1, 0, ...

  10. 使用 git 及 github

    1.github 开户.创建项目 2.以下是本地操作: #初始本地库 git init#设置git的全局邮箱和用户名git config --global user.email "<e ...