当前位置:网站首页>【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语言版
边栏推荐
- Redis + lua implements distributed interface current limiting implementation scheme
- concept noun
- ROS 话题通信理论模型
- Attitude solution - gyroscope + Euler method
- Daily sql-seek the sum of successful investments in 2016
- Find the shops that have sold more than 1,000 yuan per day for more than 30 consecutive days in the past six months
- unable to extend table xxx by 1024 in tablespace xxxx
- Multiscale communication in cortical-cortical networks
- OA project meeting notice (query & whether attending & feedback for details)
- 2022-08-10 Group 4 Self-cultivation class study notes (every day)
猜你喜欢
Discourse 的关闭主题(Close Topic )和重新开放主题
Shell:三剑客之awk
基于FPGA的FIR滤波器的实现(5)— 并行结构FIR滤波器的FPGA代码实现
MySQL使用GROUP BY 分组查询时,SELECT 查询字段包含非分组字段
一种用于EEG超扫描研究的分析流程
恒源云-Pycharm远程训练避坑指南
基于FPGA的FIR滤波器的实现(4)— 串行结构FIR滤波器的FPGA代码实现
京东商品详情API调用实例讲解
OA project meeting notice (query & whether attending & feedback for details)
亚马逊获得AMAZON商品详情 API 返回值说明
随机推荐
concept noun
【软件测试】(北京)字节跳动科技有限公司终面HR面试题
抖音API接口
语音信号处理:预处理【预加重、分帧、加窗】
What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?
强烈推荐一款好用的API接口
亚马逊获得AMAZON商品详情 API 返回值说明
EasyPlayer针对H.265视频不自动播放设置下,loading状态无法消失的解决办法
2022-08-09 Group 4 Self-cultivation class study notes (every day)
实现通用的、高性能排序和快排优化
Unity底层是如何处理C#的
maxwell concept
软件测试主要做什么工作,难不难?
ROS 话题通信理论模型
概念名词解释
一张图了解JVM八大原子操作
HCIP WPN experiment
【推荐系统】:协同过滤和基于内容过滤概述
SQL滑动窗口
矩阵分析——Jordan标准形