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 ...
随机推荐
- Android : Camera之camx hal架构
一.camx的代码结构 目前主流的机型都使用camx架构,这个架构和之前架构的主要区别就是 芯片接口层的代码从hardware/qcom 迁移到 vendor/qcom/proprietary/下面, ...
- 在eclispe上git 代码滚回
1.项目 - 鼠标右键 - team - show in history: 2.history窗口 - 选择需要回滚到的版本 - reset - hard:3.项目 - 鼠标右键 - team - p ...
- Android四大组件之Service --- 如何启动和停止Service?
启动和停止方法主要是通过Intent来实现 以上一篇中的ServiceTest项目为例来启动和停止MyService这个服务 首先修改activity_main.xml中的代码,如下所示:<Li ...
- 重新复习~ 为了重新找工作 - > XMLHttpRequest2.0 Jsonp nodeType 节点 webpack基本搭建 闭包的一句话总结
XMLHttpRequest2.0 1.可以设置超时 (xhr.timeout = 1000; ontimeout()函数) 2.支持FormData对象管理表单数据(new FormData 方法: ...
- 汉诺塔问题-递归实现-JAVA
public class hanio { /** * @param args */ public static void main(String[] args) { // TODO Auto-gene ...
- linux 免密码登陆
1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...
- easyui datagrid 后台返回所有数据,前台分页
function pagerFilter(data) { if (typeof data.length == 'number' && typeof data.splice == 'fu ...
- sprigmvc 传值jsp页面
https://blog.csdn.net/qq_41357573/article/details/84675535#如何将controller层值传递到jsp页面
- eclipse中添加jstl标签支持(引入头)
https://blog.csdn.net/wangyuxuan_java/article/details/8580318
- 作为新手在学习SSM+Easyui过程中遇到一系列问题
对于初学SSM来说,如果不熟悉SSM中SpringMVC对数据处理,会造成很大的困扰, SSM中对前台页面放在WEB-INF下,对于读取外部信息,例如导入easyui的js文件.以及不能直接进行跳转. ...