|
1 | 1 | [#0844-backspace-string-compare] |
2 | | -= 844. Backspace String Compare |
| 2 | += 844. 比较含退格的字符串 |
3 | 3 |
|
4 | | -{leetcode}/problems/backspace-string-compare/[LeetCode - Backspace String Compare^] |
| 4 | +https://leetcode.cn/problems/backspace-string-compare/[LeetCode - 844. 比较含退格的字符串^] |
5 | 5 |
|
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`。`#` 代表退格字符。 |
7 | 7 |
|
| 8 | +**注意:**如果对空文本输入退格字符,文本继续为空。 |
8 | 9 |
|
9 | | -*Example 1:* |
| 10 | +*示例 1:* |
10 | 11 |
|
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 | +.... |
47 | 17 |
|
48 | | -*Note*: |
| 18 | +*示例 2:* |
49 | 19 |
|
| 20 | +.... |
| 21 | +输入:s = "ab##", t = "c#d#" |
| 22 | +输出:true |
| 23 | +解释:s 和 t 都会变成 ""。 |
| 24 | +.... |
50 | 25 |
|
51 | | -* `1 <= S.length <= 200` |
52 | | -* `1 <= T.length <= 200` |
53 | | -* `S` and `T` only contain lowercase letters and `'#'` characters. |
| 26 | +*示例 3:* |
54 | 27 |
|
| 28 | +.... |
| 29 | +输入:s = "a#c", t = "b" |
| 30 | +输出:false |
| 31 | +解释:s 会变成 "c",但 t 仍然是 "b"。 |
| 32 | +.... |
55 | 33 |
|
56 | | -*Follow up:* |
57 | | - |
58 | | - |
59 | | -* Can you solve it in `O(N)` time and `O(1)` space? |
| 34 | +*提示:* |
60 | 35 |
|
| 36 | +* `1 \<= s.length, t.length \<= 200` |
| 37 | +* `s` 和 `t` 只含有小写字母以及字符 `#` |
61 | 38 |
|
| 39 | +*进阶:* |
62 | 40 |
|
| 41 | +* 你可以用 stem:[O(n)] 的时间复杂度和 stem:[O(1)] 的空间复杂度解决该问题吗? |
63 | 42 |
|
64 | 43 |
|
| 44 | +== 思路分析 |
65 | 45 |
|
| 46 | +如果不想占用额外的空间,可以从后向前比较:如果双方都是字母,则直接比较;否则将不是字母的下标向前推进到字母或者结束为止。 |
66 | 47 |
|
| 48 | +image::images/0844-10.gif[{image_attr}] |
67 | 49 |
|
68 | 50 | [[src-0844]] |
| 51 | +[tabs] |
| 52 | +==== |
| 53 | +一刷:: |
| 54 | ++ |
| 55 | +-- |
69 | 56 | [{java_src_attr}] |
70 | 57 | ---- |
71 | 58 | include::{sourcedir}/_0844_BackspaceStringCompare.java[tag=answer] |
72 | 59 | ---- |
| 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 | +== 参考资料 |
73 | 74 |
|
| 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. 比较含退格的字符串 - 【栈模拟】与【空间更优的双指针】详解^] |
0 commit comments