|
| 1 | +--- |
| 2 | +title: 2337. 移动片段得到字符串 |
| 3 | +description: LeetCode 2337. 移动片段得到字符串题解,Move Pieces to Obtain a String,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。 |
| 4 | +keywords: |
| 5 | + - LeetCode |
| 6 | + - 2337. 移动片段得到字符串 |
| 7 | + - 移动片段得到字符串 |
| 8 | + - Move Pieces to Obtain a String |
| 9 | + - 解题思路 |
| 10 | + - 双指针 |
| 11 | + - 字符串 |
| 12 | +--- |
| 13 | + |
| 14 | +# 2337. 移动片段得到字符串 |
| 15 | + |
| 16 | +🟠 <font color=#ffb800>Medium</font>  🔖  [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md)  🔗 [`力扣`](https://leetcode.cn/problems/move-pieces-to-obtain-a-string) [`LeetCode`](https://leetcode.com/problems/move-pieces-to-obtain-a-string) |
| 17 | + |
| 18 | +## 题目 |
| 19 | + |
| 20 | +You are given two strings `start` and `target`, both of length `n`. Each |
| 21 | +string consists **only** of the characters `'L'`, `'R'`, and `'_'` where: |
| 22 | + |
| 23 | +- The characters `'L'` and `'R'` represent pieces, where a piece `'L'` can move to the **left** only if there is a **blank** space directly to its left, and a piece `'R'` can move to the **right** only if there is a **blank** space directly to its right. |
| 24 | +- The character `'_'` represents a blank space that can be occupied by **any** of the `'L'` or `'R'` pieces. |
| 25 | + |
| 26 | +Return `true` _if it is possible to obtain the string_ `target` _by moving the |
| 27 | +pieces of the string_`start` _**any** number of times_. Otherwise, return |
| 28 | +`false`. |
| 29 | + |
| 30 | +**Example 1:** |
| 31 | + |
| 32 | +> Input: `start = "_L__R__R_", target = "L______RR"` |
| 33 | +> |
| 34 | +> Output: true |
| 35 | +> |
| 36 | +> Explanation: We can obtain the string target from start by doing the following moves: |
| 37 | +> |
| 38 | +> - Move the first piece one step to the left, start becomes equal to `"L___R__R_"`. |
| 39 | +> - Move the last piece one step to the right, start becomes equal to `"L___R___R"`. |
| 40 | +> - Move the second piece three steps to the right, start becomes equal to `"L______RR"`. |
| 41 | +> |
| 42 | +> Since it is possible to get the string target from start, we return true. |
| 43 | +
|
| 44 | +**Example 2:** |
| 45 | + |
| 46 | +> Input: `start = "R_L_", target = "__LR"` |
| 47 | +> |
| 48 | +> Output: false |
| 49 | +> |
| 50 | +> Explanation: The 'R' piece in the string start can move one step to the right to obtain `"_RL_"`. |
| 51 | +> |
| 52 | +> After that, no pieces can move anymore, so it is impossible to obtain the string target from start. |
| 53 | +
|
| 54 | +**Example 3:** |
| 55 | + |
| 56 | +> Input: `start = "_R", target = "R_"` |
| 57 | +> |
| 58 | +> Output: false |
| 59 | +> |
| 60 | +> Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start. |
| 61 | +
|
| 62 | +**Constraints:** |
| 63 | + |
| 64 | +- `n == start.length == target.length` |
| 65 | +- `1 <= n <= 10^5` |
| 66 | +- `start` and `target` consist of the characters `'L'`, `'R'`, and `'_'`. |
| 67 | + |
| 68 | +## 题目大意 |
| 69 | + |
| 70 | +给你两个字符串 `start` 和 `target` ,长度均为 `n` 。每个字符串 **仅** 由字符 `'L'`、`'R'` 和 `'_'` |
| 71 | +组成,其中: |
| 72 | + |
| 73 | +- 字符 `'L'` 和 `'R'` 表示片段,其中片段 `'L'` 只有在其左侧直接存在一个 **空位** 时才能向 **左** 移动,而片段 `'R'` 只有在其右侧直接存在一个 **空位** 时才能向 **右** 移动。 |
| 74 | +- 字符 `'_'` 表示可以被 **任意** `'L'` 或 `'R'` 片段占据的空位。 |
| 75 | + |
| 76 | +如果在移动字符串 `start` 中的片段任意次之后可以得到字符串 `target` ,返回 `true` ;否则,返回 `false` 。 |
| 77 | + |
| 78 | +**示例 1:** |
| 79 | + |
| 80 | +> **输入:** `start = "_L__R__R_", target = "L______RR"` |
| 81 | +> |
| 82 | +> **输出:** true |
| 83 | +> |
| 84 | +> **解释:** 可以从字符串 start 获得 target ,需要进行下面的移动: |
| 85 | +> |
| 86 | +> - 将第一个片段向左移动一步,字符串现在变为 `"L___R__R_"` 。 |
| 87 | +> - 将最后一个片段向右移动一步,字符串现在变为 `"L___R___R"` 。 |
| 88 | +> - 将第二个片段向右移动三步,字符串现在变为 `"L______RR"` 。 |
| 89 | +> |
| 90 | +> 可以从字符串 start 得到 target ,所以返回 true 。 |
| 91 | +
|
| 92 | +**示例 2:** |
| 93 | + |
| 94 | +> **输入:** `start = "R_L_", target = "__LR"` |
| 95 | +> |
| 96 | +> **输出:** false |
| 97 | +> |
| 98 | +> **解释:** 字符串 start 中的 'R' 片段可以向右移动一步得到 `"_RL_"` 。 |
| 99 | +> |
| 100 | +> 但是,在这一步之后,不存在可以移动的片段,所以无法从字符串 start 得到 target 。 |
| 101 | +
|
| 102 | +**示例 3:** |
| 103 | + |
| 104 | +> **输入:** `start = "_R", target = "R_"` |
| 105 | +> |
| 106 | +> **输出:** false |
| 107 | +> |
| 108 | +> **解释:** 字符串 start 中的片段只能向右移动,所以无法从字符串 start 得到 target 。 |
| 109 | +
|
| 110 | +**提示:** |
| 111 | + |
| 112 | +- `n == start.length == target.length` |
| 113 | +- `1 <= n <= 10^5` |
| 114 | +- `start` 和 `target` 由字符 `'L'`、`'R'` 和 `'_'` 组成 |
| 115 | + |
| 116 | +## 解题思路 |
| 117 | + |
| 118 | +本题要求验证是否可以通过移动 `'L'` 和 `'R'` 从字符串 `start` 变为字符串 `target`,移动规则是: |
| 119 | + |
| 120 | +1. `'L'` 可以向左移动,但不能向右; |
| 121 | +2. `'R'` 可以向右移动,但不能向左; |
| 122 | +3. `start` 和 `target` 的空位 `_` 可以被占据。 |
| 123 | + |
| 124 | +如果 `start` 移动后可以得到 `target`,必须满足以下要求: |
| 125 | + |
| 126 | +- `start` 和 `target` 的 `'L'` 和 `'R'` 的相对顺序必须一致。换句话说,`start` 和 `target` 去掉 `'_'` 后必须有相同的字符序列,否则无法通过任何移动变换得到。 |
| 127 | +- 对于 `'L'`:`start` 中的 `'L'` 的下标必须大于等于 `target` 中对应 `'L'` 的下标,因为 `'L'` 只能向左移动。 |
| 128 | +- 对于 `'R'`:`start` 中的 `'R'` 的下标必须小于等于 `target` 中对应 `'R'` 的下标,因为 `'R'` 只能向右移动。 |
| 129 | + |
| 130 | +因此,可以使用两个指针分别遍历 `start` 和 `target`,逐一验证每个 `'L'` 和 `'R'` 的位置关系是否符合上述规则。 |
| 131 | + |
| 132 | +1. 从 `start` 和 `target` 中去掉所有的 `'_'`,如果剩下的字符序列不同,直接返回 `false`。 |
| 133 | +2. 使用双指针遍历 `start` 和 `target`: |
| 134 | + - 如果遇到 `'L'`,检查 `start` 中的索引是否大于等于 `target` 中的索引。 |
| 135 | + - 如果遇到 `'R'`,检查 `start` 中的索引是否小于等于 `target` 中的索引。 |
| 136 | +3. 如果所有的字符都满足规则,返回 `true`;否则返回 `false`。 |
| 137 | + |
| 138 | +#### 复杂度分析 |
| 139 | + |
| 140 | +- **时间复杂度**:`O(n)`,其中 `n` 是字符串的长度,双指针遍历字符串 `start` 和 `target`,每个字符最多遍历一次。 |
| 141 | +- **空间复杂度**:`O(1)`,仅使用了固定数量的变量,没有额外空间开销。 |
| 142 | + |
| 143 | +## 代码 |
| 144 | + |
| 145 | +```javascript |
| 146 | +/** |
| 147 | + * @param {string} start |
| 148 | + * @param {string} target |
| 149 | + * @return {boolean} |
| 150 | + */ |
| 151 | +var canChange = function (start, target) { |
| 152 | + let n = start.length; |
| 153 | + let i = 0, |
| 154 | + j = 0; |
| 155 | + |
| 156 | + // 双指针遍历 |
| 157 | + while (i < n || j < n) { |
| 158 | + // 跳过 start 和 target 中的空位 '_' |
| 159 | + while (i < n && start[i] === '_') i++; |
| 160 | + while (j < n && target[j] === '_') j++; |
| 161 | + |
| 162 | + // 如果两者同时结束,说明匹配成功 |
| 163 | + if (i === n && j === n) return true; |
| 164 | + |
| 165 | + // 如果只有一个结束,说明匹配失败 |
| 166 | + if (i === n || j === n) return false; |
| 167 | + |
| 168 | + // 当前字符必须一致,否则匹配失败 |
| 169 | + if (start[i] !== target[j]) return false; |
| 170 | + |
| 171 | + // 验证移动规则 |
| 172 | + if (start[i] === 'L' && i < j) return false; // 'L' 不能向右移动 |
| 173 | + if (start[i] === 'R' && i > j) return false; // 'R' 不能向左移动 |
| 174 | + |
| 175 | + // 移动指针 |
| 176 | + i++; |
| 177 | + j++; |
| 178 | + } |
| 179 | + |
| 180 | + return true; |
| 181 | +}; |
| 182 | +``` |
| 183 | + |
| 184 | +## 相关题目 |
| 185 | + |
| 186 | +<!-- prettier-ignore --> |
| 187 | +| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 | |
| 188 | +| :------: | :------ | :------: | :------ | :------: | :------: | |
| 189 | +| 20 | 有效的括号 | [[✓]](/problem/0020.md) | [`栈`](/tag/stack.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/valid-parentheses) [🔗](https://leetcode.com/problems/valid-parentheses) | |
| 190 | +| 777 | 在LR字符串中交换相邻字符 | | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/swap-adjacent-in-lr-string) [🔗](https://leetcode.com/problems/swap-adjacent-in-lr-string) | |
0 commit comments