当前位置:网站首页>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 .
边栏推荐
猜你喜欢

数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】

LED light of single chip microcomputer learning notes

Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules

118. Yanghui triangle

Distributed transaction solution

我那“不好惹”的00后下属:不差钱,怼领导,抵制加班

cmake 学习使用笔记(一)

2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来

干货|总结那些漏洞工具的联动使用

xshell连接服务器把密钥登陆改为密码登陆
随机推荐
ESP32 ① 编译环境
Final review notes of single chip microcomputer principle
记一次 .NET 某新能源系统 线程疯涨 分析
OSI seven layer model
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
[1] ROS2基础知识-操作命令总结版
Esp32 ① compilation environment
Getting started with MySQL
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
How did Guotai Junan Securities open an account? Is it safe to open an account?
2022-7-7 Leetcode 844.比较含退格的字符串
Solve the cache breakdown problem
Split screen bug notes
信号强度(RSSI)知识整理
Thread pool reject policy best practices
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
一文读懂数仓中的pg_stat
Deep understanding of array related problems in C language
室内ROS机器人导航调试记录(膨胀半径的选取经验)
Simple and easy-to-use code specification