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 ...
随机推荐
- 在win中,给powershell客户端,搭建sshd服务器。
下载:https://github.com/PowerShell/Win32-OpenSSH/releases 问:为什么要用这个sshd?答:这是微软用,openssh官方的源码,源码网址: ...
- ffmpeg使用示例
/* 视频格式转换 ffmpeg -i "F:\Test\1.mp4" -y -vcodec copy -acodec copy "F:\Test\11.avi" ...
- Python知识体系思维导图:
基础知识 数据类型 1.序列 2.字符串 3.列表和元组 4.字典和集合 循环 & 判断
- Oracle分析函数及常用函数: over(),rank()over()作用及用法--分区(分组)求和& 不连续/连续排名
(1) 函数: over()的作用及用法: -- 分区(分组)求和. sum() over( partition by column1 order by column2 )主要用来对某个字 ...
- JavaWeb基础-Jsp的请求与响应
JSP请求和相应 HTTP头信息 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的. HttpServletRequest类 r ...
- SQL server 一些小结
数据库表常用术语 关系 关系即二维表,每一个关系都有一个关系名,就是表名记录 表中的行字段 表中的列 也称属性域 取值范围关联 不同数据库表之间的数据联系关键字 属性或属性的组合,可以用于唯一标识一条 ...
- 淘宝的推荐系统 计算之道2A
小明刚刚入职淘宝,老大给他交代了一个简单的任务,实现一个简易的商品推荐系统. 这个商品推荐系统的需求如下: 一共有 n 件商品可以被推荐,他们的编号分别为 1 到 n.每件商品都有一个价格,编号为 i ...
- 五、stdout,stdoin和stderr
stdout,stdin和stderr分别是标准输出流.标准输入流和标准错误流,当一个用户进程被创建的时候,系统会自动为该进程创建这三个数据流,默认情况下这三个流是在终端上表现出来的.可以使用fpri ...
- 精进之路之HashMap
HashMap本质的核心就是“数组+链表”,数组对于访问速度很快,而链表的优势在于插入速度快,HashMap集二者于一身. 提到HashMap,我们不得不提各个版本对于HashMap的不同.本文中先从 ...
- 2Sum问题
2Sum问题是3Sum和4Sum的基础,很多OJ都是以此为最简单的练手题的. 题目描述: 从一个数组里找出两个和为target的数. LeetCode上的描述: Given an array of i ...