LeetCode - Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".
Example 2: Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".
Example 3: Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".
Example 4: Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".
Note: 1 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and '#' characters.
Follow up: Can you solve it in O(N) time and O(1) space?
注意,这道题都是需要 time O(n) 和 space O(1)的,所以需要用 two pointer来做,注意最后结束条件:
class Solution {
public boolean backspaceCompare(String S, String T) {
if(S == null || T == null){
return false;
}
int i1 = S.length()-1;
int count1= 0;
int i2 = T.length()-1;
int count2 = 0;
while(i1 >= 0 || i2 >= 0){
if(i1 >= 0 && S.charAt(i1) == '#'){
count1++;
i1--;
}
else if (i2 >= 0 && T.charAt(i2) == '#'){
count2++;
i2--;
}
else if( i1 >= 0 && S.charAt(i1) != '#' && count1 > 0){
count1--;
i1--;
}
else if(i2 >= 0 && T.charAt(i2) != '#' && count2 > 0){
count2--;
i2--;
}
else{
if( i1>=0 && i2 >= 0 && S.charAt(i1) == T.charAt(i2) ){
i1--;
i2--;
}
else{
return false;
}
}
}
return true;
}
}
LeetCode - Backspace String Compare的更多相关文章
- [LeetCode] Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- [LeetCode] 844. Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- LeetCode算法题-Backspace String Compare(Java实现)
这是悦乐书的第327次更新,第350篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第197题(顺位题号是844).给定两个字符串S和T,如果两个字符串都输入到空文本编辑器 ...
- C#LeetCode刷题之#844-比较含退格的字符串(Backspace String Compare)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- [LeetCode&Python] Problem 844. Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- [Swift]LeetCode844. 比较含退格的字符串 | Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 844. Backspace String Compare判断删除后的结果是否相等
[抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...
随机推荐
- oracle问题 ORA-01843:not a valid month
解决思路: 开始解决问题走了些弯路,搜了一些资料,结果大部分说的是修改会话的nls_date_language参数 可是线上正式项目,不能说改就改吧 就找其他方式解决 最终找到问题,to_date() ...
- github下载项目代码到本地,不能运行 本地改完代码 再上传
首先用git bash here,在指定目录下执行, git clone 将项目拉取下来, 试运行: 发现需要配置idea的SDK/jdk, 还要选择language level, 建立输出目录tar ...
- windows下启动和运行分布式消息中间件消息队列 kafka
本文转载至:https://www.cnblogs.com/flower1990/p/7466882.html 一.安装JAVA JDK 1.下载安装包 http://www.oracle.com/t ...
- 动态规划———最长公共子序列(LCS)
最长公共子序列+sdutoj2080改编: http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/ ...
- 第六次作业———numpy数据集练习
1. 安装scipy,numpy,sklearn包 2. 从sklearn包自带的数据集中读出鸢尾花数据集data 3.查看data类型,包含哪些数据 4.取出鸢尾花特征和鸢尾花类别数据,查看其形状及 ...
- mybatis中<foreach>标签的使用
标签太多,记一下不是特别常用的标签~留着脑袋瓜不机灵的时候看! <foreach>标签 该标签的作用是遍历集合类型的条件 <select id="countByUserL ...
- 简述我理解的C#
第一章:开发入门 一.基础知识 1.计算机语言发展进程:计算机语言大致经过了机器语言.汇编语言.高级语言三个阶段的发展历程. 汇编语言:使用助记符来替代机器指令机器语言这种反人类的设计,阻碍了软 ...
- Hadoop Hive HBase Spark Storm概念解释
HadoopHadoop是什么? 答:一个分布式系统基础架构. Hadoop解决了什么问题? 答:解决了大数据(大到一台计算机无法进行存储,一台计算机无法在要求的时间内进行处理)的可靠存储(HDFS) ...
- Holer实现外网访问本地MySQL数据库
外网访问内网MySQL数据库 内网主机上安装了MySQL数据库,只能在局域网内访问,怎样从公网也能访问本地MySQL数据库? 本文将介绍使用holer实现的具体步骤. 1. 准备工作 1.1 安装并启 ...
- element 多个文件上传多次http请求解决方法
第一步 action="#" 第二步 :auto-upload = "false" 第三步 给元素绑定click事件触发提交方法,注意,把其他没有用的文件都给去 ...