当前位置:网站首页>【LeetCode每日一题】——844.比较含退格的字符串
【LeetCode每日一题】——844.比较含退格的字符串
2022-08-11 06:28:00 【IronmanJay】
一【题目类别】
- 栈
二【题目难度】
- 简单
三【题目编号】
- 844.比较含退格的字符串
四【题目描述】
- 给定 s 和 t 两个字符串,当它们分别被输入到空白的文本编辑器后,如果两者相等,返回 true 。# 代表退格字符。
- 注意:如果对空文本输入退格字符,文本继续为空。
五【题目示例】
示例 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 = “b”
- 输出:false
- 解释:s 会变成 “c”,但 t 仍然是 “b”。
六【题目提示】
- 1 <= s.length, t.length <= 200
- s 和 t 只含有小写字母以及字符 ‘#’
七【题目进阶】
- 进阶:你可以用 O ( n ) O(n) O(n) 的时间复杂度和 O ( 1 ) O(1) O(1) 的空间复杂度解决该问题吗?
八【解题思路】
- 利用栈的思想
- 遇到不是‘#’就入栈
- 遇到‘#’如果栈不空就退栈,如果栈空不进行操作
- 最后先比较栈长度,如果长度不相同肯定不是相同的字符串
- 然后一一比较栈元素,如果有不相同的肯定也不是相同的字符串
- 如果都没问题就是相同的字符串
九【时间频度】
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组元素个数
- 空间复杂度:C语言为 O ( 1 ) O(1) O(1),Java语言为 O ( n ) O(n) O(n),其中 n n n 是数组元素个数
十【代码实现】
- Java语言版
package Stack;
import java.util.Stack;
public class p844_CompareStringsWithBackspaces {
public static void main(String[] args) {
String s = "ab#c";
String t = "ad#c";
boolean res = backspaceCompare(s, t);
System.out.println("res = " + res);
}
public static boolean backspaceCompare(String s, String t) {
Stack stackS = new Stack();
Stack stackT = new Stack();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '#') {
stackS.push(s.charAt(i));
} else {
if (!stackS.isEmpty()) {
stackS.pop();
} else {
continue;
}
}
}
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) != '#') {
stackT.push(t.charAt(i));
} else {
if (!stackT.isEmpty()) {
stackT.pop();
} else {
continue;
}
}
}
if (stackS.size() != stackT.size()) {
return false;
} else {
while (!stackS.isEmpty()) {
if (stackS.pop() != stackT.pop()) {
return false;
}
}
}
return true;
}
}
- C语言版
#include<stdio.h>
#include<stdbool.h>
bool backspaceCompare(char * s, char * t)
{
int topS = -1;
int topT = -1;
for (int i = 0; i < strlen(s); i++)
{
if (s[i] != '#')
{
s[++topS] = s[i];
}
else
{
if (topS != -1)
{
topS--;
}
else
{
topS = -1;
}
}
}
for (int i = 0; i < strlen(t); i++)
{
if (t[i] != '#')
{
t[++topT] = t[i];
}
else
{
if (topT != -1)
{
topT--;
}
else
{
topT = -1;
}
}
}
if (topS != topT)
{
return false;
}
else
{
for (int i = 0; i <= topS; i++)
{
if (s[i] != t[i])
{
return false;
}
}
}
return true;
}
/*主函数省略*/
十一【提交结果】
Java语言版

C语言版

边栏推荐
- Do not add the is prefix to the variables of the boolean type in the POJO class of the Alibaba specification
- 技能在赛题解析:交换机防环路设置
- Resolved EROR 1064 (42000): You have an error in. your SOL syntax. check the manual that corresponds to yo
- 每日sql:求好友申请通过率
- 为什么我使用C#操作MySQL进行中文查询失败
- Production and optimization of Unity game leaderboards
- OA project meeting notice (query & whether attending & feedback for details)
- 淘宝商品详情API接口
- 拼多多api接口应用示例
- 皮质-皮质网络的多尺度交流
猜你喜欢

Daily sql--statistics the total salary of employees in the past three months (excluding the latest month)

ROS 话题通信理论模型

unable to extend table xxx by 1024 in tablespace xxxx

皮质-皮质网络的多尺度交流

Trill keyword search goods - API

exness:黄金1800关口遇阻,静待美国CPI出炉

Douyin share password url API tool

技术分享 | 实战演练接口自动化如何处理 Form 请求?

详述MIMIC 的ICU患者检测时间信息表(十六)

抖音关键词搜索商品-API工具
随机推荐
每日sql -用户两天留存率
京东商品详情API调用实例讲解
技术分享 | 实战演练接口自动化如何处理 Form 请求?
【预约观看】Ambire 智能钱包 AMA 活动第四期即将举行
Daily sql: request for friend application pass rate
【软件测试】(北京)字节跳动科技有限公司二面笔试题
Go语言实现Etcd服务发现(Etcd & Service Discovery & Go)
每日sql:求好友申请通过率
2022-08-09 Group 4 Self-cultivation class study notes (every day)
1688商品详情接口
maxwell concept
软件测试基本流程有哪些?北京专业第三方软件检测机构安利
SQL sliding window
Pytorch模型转ONNX模型
Edge provides label grouping functionality
恒源云-Pycharm远程训练避坑指南
Taobao sku API interface (PHP example)
Redis源码:Redis源码怎么查看、Redis源码查看顺序、Redis外部数据结构到Redis内部数据结构查看源码顺序
姿态解算-陀螺仪+欧拉法
MySQL 版本升级心得