当前位置:网站首页>2022-7-7 Leetcode 844. Compare strings with backspace
2022-7-7 Leetcode 844. Compare strings with backspace
2022-07-07 13:39:00 【weixin_ fifty-one million one hundred and eighty-seven thousand】


Method 1 : Stack
class Solution {
public:
bool backspaceCompare(string s, string t) {
string tmps = "", tmpt = "";
for (auto& ch:s){
if (ch == '#'){
if (!tmps.empty())
tmps.pop_back();
}else tmps.push_back(ch);
}
for (auto& ch:t){
if (ch == '#'){
if (!tmpt.empty())
tmpt.pop_back();
}else tmpt.push_back(ch);
}
return tmps == tmpt;
}
};
Method 2 : Double pointer
class Solution {
public:
bool backspaceCompare(string S, string T) {
int i = S.length() - 1, j = T.length() - 1;
int skipS = 0, skipT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0) {
if (S[i] == '#') {
skipS++, i--;
} else if (skipS > 0) {
skipS--, i--;
} else {
break;
}
}
while (j >= 0) {
if (T[j] == '#') {
skipT++, j--;
} else if (skipT > 0) {
skipT--, j--;
} else {
break;
}
}
if (i >= 0 && j >= 0) {
if (S[i] != T[j]) {
return false;
}
} else {
if (i >= 0 || j >= 0) {
return false;
}
}
i--, j--;
}
return true;
}
};
author :demigodliu
link :https://leetcode.cn/problems/backspace-string-compare/solution/shuang-zhi-zhen-bi-jiao-han-tui-ge-de-zi-8fn8/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- Distributed transaction solution
- 2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
- LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
- PAcP learning note 1: programming with pcap
- Summary of import, export, backup and recovery of mongodb
- QQ medicine, Tencent ticket
- JS slow motion animation principle teaching (super detail)
- 数字ic设计——SPI
- clion mingw64中文乱码
- 2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
猜你喜欢

LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)

Navicat运行sql文件导入数据不全或导入失败

1、深拷贝 2、call apply bind 3、for of for in 区别

Redis只能做缓存?太out了!

Final review notes of single chip microcomputer principle
![[dark horse morning post] Huawei refutes rumors about](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours

Introduce six open source protocols in detail (instructions for programmers)

【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

QQ medicine, Tencent ticket
随机推荐
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Digital IC Design SPI
Drawerlayout suppress sideslip display
Custom thread pool rejection policy
648. 单词替换 : 字典树的经典运用
[daily training] 648 Word replacement
作战图鉴:12大场景详述容器安全建设要求
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
高端了8年,雅迪如今怎么样?
Vscade editor esp32 header file wavy line does not jump completely solved
[Presto profile series] timeline use
JS slow motion animation principle teaching (super detail)
【日常训练--腾讯精选50】231. 2 的幂
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
得物客服热线的演进之路
Deep understanding of array related problems in C language
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)