当前位置:网站首页>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 .
边栏推荐
- TPG x AIDU | AI leading talent recruitment plan in progress!
- Ogre introduction
- [QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
- "Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
- 【堡垒机】云堡垒机和普通堡垒机的区别是什么?
- Mongodb replication (replica set) summary
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
- "New red flag Cup" desktop application creativity competition 2022
- MongoDB内部的存储原理
- LeetCode简单题分享(20)
猜你喜欢
存储过程的介绍与基本使用
靠卖概念上市,认养一头牛能走多远?
Flink | multi stream conversion
"New red flag Cup" desktop application creativity competition 2022
Deep understanding of array related problems in C language
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
Getting started with cinnamon applet
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Cmake learning and use notes (1)
Storage principle inside mongodb
随机推荐
ROS机器人更换新雷达需要重新配置哪些参数
Esp32 series column
Pcap learning notes II: pcap4j source code Notes
Custom thread pool rejection policy
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
Help tenants
Milkdown control icon
Summary of import, export, backup and recovery of mongodb
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
How to make join run faster?
xshell连接服务器把密钥登陆改为密码登陆
MySQL error 28 and solution
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
[daily training -- Tencent select 50] 231 Power of 2
简单好用的代码规范
[Presto profile series] timeline use
Mongodb replication (replica set) summary
Why can basic data types call methods in JS
《厌女:日本的女性嫌恶》摘录
室内ROS机器人导航调试记录(膨胀半径的选取经验)