当前位置:网站首页>844. Compare strings with backspace
844. Compare strings with backspace
2022-07-21 22:40:00 【Anji_ lh1029】
1、 Title Description
2、 title
Double pointer traversal method
Thinking analysis :
- 1、 Prepare two pointers i, j Point to respectively S,T Last character of , Prepare two more variables skipS,skipT To store... Separately S,T In the string # Number .
- 2、 Go back and forth S, There are three situations , As shown below :
- 2.1 If the current character is #, be skipS Self increasing 1;
- 2.2 If the current character is not #, And skipS Not for 0, be skipS Self reduction 1;
- 2.3 If the current character is not #, And skipS by 0, It means that the current character will not be eliminated , We can use it with T Compare the current character in the .
- If the comparison process occurs S, TT The current character does not match , Then the traversal ends , return false, if S,T All traversal ends , And can match one by one , Then return to true.
class Solution {
public boolean backspaceCompare(String s, String t) {
int i = s.length()-1;
int j = t.length()-1;
int skipS = 0; int skipT = 0;
// Only i perhaps j There is one >= 0, Check the space , Data comparison
// remarks : Extremely important , Don't write it while(i > 0 || j > 0)
while(i >= 0 || j >= 0){
// Yes s String to check , All spaces are currently involved ‘#’ Uniform backspace
while( i >= 0 ){
if(s.charAt(i)=='#'){
i--;
skipS++;
}else if(skipS > 0){
i--;
skipS--;
}else{
break;
}
}
// Yes t String to check , All spaces are currently involved ‘#’ Uniform backspace
while( j >= 0){
if(t.charAt(j)=='#'){
j--;
skipT++;
}else if(skipT > 0){
j--;
skipT--;
}else{
break;
}
}
if (i >= 0 && j >= 0) {
if (s.charAt(i) != t.charAt(j)) return false;
} else if (i >= 0 || j >= 0) {
// One of them has traversed the whole string , But the other party didn't traverse the whole string , Go straight back to false
return false;
}
i--;
j--;
}
return true;
}
}
Complexity analysis
Time complexity :O(N+M), among N and M They are strings S and T The length of . We need to traverse both strings once .
Spatial complexity :O(1). For each string , We just need to define a pointer and a counter .
remarks : This question is 【JAVA】 Implementation code ( The above code ), There are many details , When compiling the self-test , Often case Compile exception , For example, the notes say :
1、 while(i >= 0 || j >= 0) // Only i perhaps j There is one >= 0, Check the space , Data comparison
remarks : Extremely important , Don't write it while ( i > 0 || j > 0 )
2、 while The last check in ,
if (i >= 0 && j >= 0) {
if (s.charAt(i) != t.charAt(j)) return false;
} else if (i >= 0 || j >= 0) {
// One of them has traversed the whole string , But the other party didn't traverse the whole string , Go straight back to false
return false;
}
Specifically for the following case Of , When submitting the code , Often fail to measure !
/** "bbbextm" "bbb#extm" Output :true Expected results :false */
边栏推荐
- What is an electronic scale weighing module? What functions does it have?
- 干货 | 分布式系统的数据分片难题
- 流批一体?实时数据处理场景化应用实例~
- This Bluetooth chip giant aims at the WiFi SOC market and launches a low-power WiFi MCU product line
- Programmation créative / groupe primaire (4e - 6e année) - graphisme créatif
- 概率论-假设检验
- 234. 回文链表
- 线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
- One article explains the problem of data fragmentation in distributed systems
- What is the Internet of things control system? What are its characteristics?
猜你喜欢
线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
234. 回文链表
This Bluetooth chip giant aims at the WiFi SOC market and launches a low-power WiFi MCU product line
2020普及组总结
datart 开源数据可视化应用 | 手把手教你开发出优秀的图表插件
2021普及组总结
What is the gateway? What is the Internet of things gateway?
Heap - principle to application - heap sorting, priority queue
Deep analysis of fiboracci sequence
004: print characters
随机推荐
Good looking and interesting data visualization chart making, worship tutorial
VisualStudio2019 配置点云库 PCL1.11.1+斯坦福兔子测试
226. 翻转二叉树
Which of these five special Bluetooth cores suits your application best
22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
n的阶乘
Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
Analysis of the characteristics of matter protocol (I) support non matter protocol, private protocol, and technical analysis of matter Bridge
beta.4 版发布啦,国产开源数据可视化 datart 接下来将会小跑进入 rc 阶段
Heap - principle to application - heap sorting, priority queue
In depth analysis of multiple knapsack problem (Part 2)
328. 奇偶链表
概率论-最大似然估计
第七周ACM训练报告
D - AND and SUM (AtCoder Beginner Contest 238)
西瓜书第二章-比较检验
006: floating point data type storage space size
概率论-大数定律和中心极限定理
第三周ACM训练报告
Let you know the current situation and future development trend of wireless charging technology