当前位置:网站首页>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 .
边栏推荐
- MySQL error 28 and solution
- Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
- JNA learning notes 1: Concepts
- OSI seven layer model
- 【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
- High end for 8 years, how is Yadi now?
- Data refresh of recyclerview
- OSI 七层模型
- 2022-7-7 Leetcode 844.比较含退格的字符串
- Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
猜你喜欢
118. 杨辉三角
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
flask session伪造之hctf admin
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
MySQL error 28 and solution
2022-7-7 Leetcode 844.比较含退格的字符串
[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
数字ic设计——SPI
作战图鉴:12大场景详述容器安全建设要求
Thread pool reject policy best practices
随机推荐
MongoDB优化的几点原则
MongoDB复制(副本集)总结
Ways to improve the performance of raspberry pie
Server to server (S2S) event (adjust)
记一次 .NET 某新能源系统 线程疯涨 分析
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
供应链供需预估-[时间序列]
Why can basic data types call methods in JS
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
一文读懂数仓中的pg_stat
Error lnk2019: unresolved external symbol
[daily training -- Tencent select 50] 231 Power of 2
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
OSI 七层模型
提升树莓派性能的方法
.net core 关于redis的pipeline以及事务
PAcP learning note 3: pcap method description
干货|总结那些漏洞工具的联动使用