Skip to content

Commit c805636

Browse files
committed
一刷844
1 parent 102dd04 commit c805636

6 files changed

Lines changed: 133 additions & 58 deletions

File tree

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5932,14 +5932,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
59325932
//|{doc_base_url}/0843-guess-the-word.adoc[题解]
59335933
//|Hard
59345934
//|
5935-
//
5936-
//|{counter:codes}
5937-
//|{leetcode_base_url}/backspace-string-compare/[844. Backspace String Compare^]
5938-
//|{source_base_url}/_0844_BackspaceStringCompare.java[Java]
5939-
//|{doc_base_url}/0844-backspace-string-compare.adoc[题解]
5940-
//|Easy
5941-
//|
5942-
//
5935+
5936+
|{counter:codes}
5937+
|{leetcode_base_url}/backspace-string-compare/[844. Backspace String Compare^]
5938+
|{source_base_url}/_0844_BackspaceStringCompare.java[Java]
5939+
|{doc_base_url}/0844-backspace-string-compare.adoc[题解]
5940+
|Easy
5941+
|
5942+
59435943
//|{counter:codes}
59445944
//|{leetcode_base_url}/longest-mountain-in-array/[845. Longest Mountain in Array^]
59455945
//|{source_base_url}/_0845_LongestMountainInArray.java[Java]
Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,77 @@
11
[#0844-backspace-string-compare]
2-
= 844. Backspace String Compare
2+
= 844. 比较含退格的字符串
33

4-
{leetcode}/problems/backspace-string-compare/[LeetCode - Backspace String Compare^]
4+
https://leetcode.cn/problems/backspace-string-compare/[LeetCode - 844. 比较含退格的字符串^]
55

6-
Given two strings `S` and `T`, return if they are equal when both are typed into empty text editors. `#` means a backspace character.
6+
给定 `s``t` 两个字符串,当它们分别被输入到空白的文本编辑器后,如果两者相等,返回 `true``#` 代表退格字符。
77

8+
**注意:**如果对空文本输入退格字符,文本继续为空。
89

9-
*Example 1:*
10+
*示例 1:*
1011

11-
[subs="verbatim,quotes,macros"]
12-
----
13-
*Input:* S = "ab#c", T = "ad#c"
14-
*Output:* true
15-
*Explanation*: Both S and T become "ac".
16-
----
17-
18-
19-
*Example 2:*
20-
21-
[subs="verbatim,quotes,macros"]
22-
----
23-
*Input:* S = "ab##", T = "c#d#"
24-
*Output:* true
25-
*Explanation*: Both S and T become "".
26-
----
27-
28-
29-
*Example 3:*
30-
31-
[subs="verbatim,quotes,macros"]
32-
----
33-
*Input:* S = "a##c", T = "#a#c"
34-
*Output:* true
35-
*Explanation*: Both S and T become "c".
36-
----
37-
38-
39-
*Example 4:*
40-
41-
[subs="verbatim,quotes,macros"]
42-
----
43-
*Input:* S = "a#c", T = "b"
44-
*Output:* false
45-
*Explanation*: S becomes "c" while T becomes "b".
46-
----
12+
....
13+
输入:s = "ab#c", t = "ad#c"
14+
输出:true
15+
解释:s 和 t 都会变成 "ac"。
16+
....
4717

48-
*Note*:
18+
*示例 2:*
4919

20+
....
21+
输入:s = "ab##", t = "c#d#"
22+
输出:true
23+
解释:s 和 t 都会变成 ""。
24+
....
5025

51-
* `1 <= S.length <= 200`
52-
* `1 <= T.length <= 200`
53-
* `S` and `T` only contain lowercase letters and `'#'` characters.
26+
*示例 3:*
5427

28+
....
29+
输入:s = "a#c", t = "b"
30+
输出:false
31+
解释:s 会变成 "c",但 t 仍然是 "b"。
32+
....
5533

56-
*Follow up:*
57-
58-
59-
* Can you solve it in `O(N)` time and `O(1)` space?
34+
*提示:*
6035

36+
* `1 \<= s.length, t.length \<= 200`
37+
* `s``t` 只含有小写字母以及字符 `#`
6138
39+
*进阶:*
6240

41+
* 你可以用 stem:[O(n)] 的时间复杂度和 stem:[O(1)] 的空间复杂度解决该问题吗?
6342
6443
44+
== 思路分析
6545

46+
如果不想占用额外的空间,可以从后向前比较:如果双方都是字母,则直接比较;否则将不是字母的下标向前推进到字母或者结束为止。
6647

48+
image::images/0844-10.gif[{image_attr}]
6749

6850
[[src-0844]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
6956
[{java_src_attr}]
7057
----
7158
include::{sourcedir}/_0844_BackspaceStringCompare.java[tag=answer]
7259
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0844_BackspaceStringCompare_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
7374

75+
. https://leetcode.cn/problems/backspace-string-compare/solutions/451606/bi-jiao-han-tui-ge-de-zi-fu-chuan-by-leetcode-solu/[844. 比较含退格的字符串 - 官方题解^]
76+
. https://leetcode.cn/problems/backspace-string-compare/solutions/683776/shuang-zhi-zhen-bi-jiao-han-tui-ge-de-zi-8fn8/[844. 比较含退格的字符串 - 【双指针】比较含退格的字符串^]
77+
. https://leetcode.cn/problems/backspace-string-compare/solutions/451967/844zhan-mo-ni-yu-kong-jian-geng-you-de-shuang-zhi-/[844. 比较含退格的字符串 - 【栈模拟】与【空间更优的双指针】详解^]

docs/images/0844-10.gif

1.2 MB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ include::0842-split-array-into-fibonacci-sequence.adoc[leveloffset=+1]
17801780

17811781
// include::0843-guess-the-word.adoc[leveloffset=+1]
17821782

1783-
// include::0844-backspace-string-compare.adoc[leveloffset=+1]
1783+
include::0844-backspace-string-compare.adoc[leveloffset=+1]
17841784

17851785
// include::0845-longest-mountain-in-array.adoc[leveloffset=+1]
17861786

logbook/202601.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,10 +1097,14 @@ endif::[]
10971097
|✅ 模拟计算题。
10981098

10991099
|{counter:codes}
1100-
|{leetcode_base_url}/split-array-into-fibonacci-sequence/[842. Split Array into Fibonacci Sequence^]
1100+
|{leetcode_base_url}/split-array-into-fibonacci-sequence/[842. 将数组拆分成斐波那契序列^]
11011101
|{doc_base_url}/0842-split-array-into-fibonacci-sequence.adoc[题解]
11021102
|✅ 回溯。将字符串分割成数字,逐个检查是否可以组成斐波那契数列,不符合要求就回溯。
11031103

1104+
|{counter:codes}
1105+
|{leetcode_base_url}/backspace-string-compare/[844. 比较含退格的字符串^]
1106+
|{doc_base_url}/0844-backspace-string-compare.adoc[题解]
1107+
|✅ 双指针。从后向前比较:如果双方都是字母,则直接比较;否则将不是字母的下标向前推进到字母或者结束为止。
11041108

11051109
|===
11061110

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0844_BackspaceStringCompare {
4+
// tag::answer[]
5+
/**
6+
* 代码写的好丑!
7+
*
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2026-08-01 21:10:29
10+
*/
11+
public boolean backspaceCompare(String s, String t) {
12+
int si = s.length() - 1;
13+
int ti = t.length() - 1;
14+
int sj = 0, tj = 0;
15+
while (si >= 0 || ti >= 0) {
16+
if (si >= 0 && s.charAt(si) != '#'
17+
&& ti >= 0 && t.charAt(ti) != '#'
18+
&& s.charAt(si) != t.charAt(ti)) {
19+
return false;
20+
}
21+
while (sj > 0 || (si >= 0 && s.charAt(si) == '#')) {
22+
if (si >= 0 && s.charAt(si) != '#' && sj > 0) {
23+
si--;
24+
sj--;
25+
} else if (si >= 0 && s.charAt(si) == '#') {
26+
si--;
27+
sj++;
28+
}
29+
if (si < 0) {
30+
break;
31+
}
32+
}
33+
while (tj > 0 || (ti >= 0 && t.charAt(ti) == '#')) {
34+
if (ti >= 0 && t.charAt(ti) != '#' && tj > 0) {
35+
ti--;
36+
tj--;
37+
} else if (ti >= 0 && t.charAt(ti) == '#') {
38+
ti--;
39+
tj++;
40+
}
41+
if (ti < 0) {
42+
break;
43+
}
44+
}
45+
if (si >= 0 && ti >= 0 && s.charAt(si) != t.charAt(ti)) {
46+
return false;
47+
} else if (si < 0 && ti < 0) {
48+
break;
49+
} else if (si < 0 || ti < 0) {
50+
return false;
51+
}
52+
si--;
53+
ti--;
54+
}
55+
return true;
56+
}
57+
// end::answer[]
58+
59+
static void main() {
60+
new _0844_BackspaceStringCompare()
61+
.backspaceCompare("nzp#o#g", "b#nzp#o#g");
62+
// .backspaceCompare("bxj##tw", "bxj###tw");
63+
// .backspaceCompare("a##c", "#a#c");
64+
// .backspaceCompare("ab##", "c#d#");
65+
// .backspaceCompare("ab#c", "ad#c");
66+
}
67+
}

0 commit comments

Comments
 (0)