|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3460. Longest Common Prefix After at Most One Removal 🔒](https://leetcode.cn/problems/longest-common-prefix-after-at-most-one-removal) |
| 10 | + |
| 11 | +[English Version](/solution/3400-3499/3460.Longest%20Common%20Prefix%20After%20at%20Most%20One%20Removal/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given two strings <code>s</code> and <code>t</code>.</p> |
| 18 | + |
| 19 | +<p>Return the <strong>length</strong> of the <strong>longest common <span data-keyword="string-prefix">prefix</span></strong> between <code>s</code> and <code>t</code> after removing <strong>at most</strong> one character from <code>s</code>.</p> |
| 20 | + |
| 21 | +<p><strong>Note:</strong> <code>s</code> can be left without any removal.</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">s = "madxa", t = "madam"</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<p>Removing <code>s[3]</code> from <code>s</code> results in <code>"mada"</code>, which has a longest common prefix of length 4 with <code>t</code>.</p> |
| 34 | +</div> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<div class="example-block"> |
| 39 | +<p><strong>Input:</strong> <span class="example-io">s = "leetcode", t = "eetcode"</span></p> |
| 40 | + |
| 41 | +<p><strong>Output:</strong> <span class="example-io">7</span></p> |
| 42 | + |
| 43 | +<p><strong>Explanation:</strong></p> |
| 44 | + |
| 45 | +<p>Removing <code>s[0]</code> from <code>s</code> results in <code>"eetcode"</code>, which matches <code>t</code>.</p> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p><strong class="example">Example 3:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong> <span class="example-io">s = "one", t = "one"</span></p> |
| 52 | + |
| 53 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong></p> |
| 56 | + |
| 57 | +<p>No removal is needed.</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p><strong class="example">Example 4:</strong></p> |
| 61 | + |
| 62 | +<div class="example-block"> |
| 63 | +<p><strong>Input:</strong> <span class="example-io">s = "a", t = "b"</span></p> |
| 64 | + |
| 65 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 66 | + |
| 67 | +<p><strong>Explanation:</strong></p> |
| 68 | + |
| 69 | +<p><code>s</code> and <code>t</code> cannot have a common prefix.</p> |
| 70 | +</div> |
| 71 | + |
| 72 | +<p> </p> |
| 73 | +<p><strong>Constraints:</strong></p> |
| 74 | + |
| 75 | +<ul> |
| 76 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 77 | + <li><code>1 <= t.length <= 10<sup>5</sup></code></li> |
| 78 | + <li><code>s</code> and <code>t</code> contain only lowercase English letters.</li> |
| 79 | +</ul> |
| 80 | + |
| 81 | +<!-- description:end --> |
| 82 | + |
| 83 | +## 解法 |
| 84 | + |
| 85 | +<!-- solution:start --> |
| 86 | + |
| 87 | +### 方法一:双指针 |
| 88 | + |
| 89 | +我们记录字符串 $s$ 和 $t$ 的长度分别为 $n$ 和 $m$,然后用两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 和 $t$ 的开头,用一个布尔变量 $\textit{rem}$ 记录是否已经删除过字符。 |
| 90 | + |
| 91 | +接下来,我们开始遍历字符串 $s$ 和 $t$,如果 $s[i]$ 不等于 $t[j]$,我们就判断是否已经删除过字符,如果已经删除过字符,我们就退出循环,否则我们标记已经删除过字符,然后跳过 $s[i]$;否则,我们跳过 $s[i]$ 和 $t[j]$。继续遍历,直到 $i \geq n$ 或 $j \geq m$。 |
| 92 | + |
| 93 | +最后返回 $j$ 即可。 |
| 94 | + |
| 95 | +时间复杂度 $O(n+m)$,其中 $n$ 和 $m$ 分别是字符串 $s$ 和 $t$ 的长度。 |
| 96 | + |
| 97 | +<!-- tabs:start --> |
| 98 | + |
| 99 | +#### Python3 |
| 100 | + |
| 101 | +```python |
| 102 | +class Solution: |
| 103 | + def longestCommonPrefix(self, s: str, t: str) -> int: |
| 104 | + n, m = len(s), len(t) |
| 105 | + i = j = 0 |
| 106 | + rem = False |
| 107 | + while i < n and j < m: |
| 108 | + if s[i] != t[j]: |
| 109 | + if rem: |
| 110 | + break |
| 111 | + rem = True |
| 112 | + else: |
| 113 | + j += 1 |
| 114 | + i += 1 |
| 115 | + return j |
| 116 | +``` |
| 117 | + |
| 118 | +#### Java |
| 119 | + |
| 120 | +```java |
| 121 | +class Solution { |
| 122 | + public int longestCommonPrefix(String s, String t) { |
| 123 | + int n = s.length(), m = t.length(); |
| 124 | + int i = 0, j = 0; |
| 125 | + boolean rem = false; |
| 126 | + while (i < n && j < m) { |
| 127 | + if (s.charAt(i) != t.charAt(j)) { |
| 128 | + if (rem) { |
| 129 | + break; |
| 130 | + } |
| 131 | + rem = true; |
| 132 | + } else { |
| 133 | + ++j; |
| 134 | + } |
| 135 | + ++i; |
| 136 | + } |
| 137 | + return j; |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +#### C++ |
| 143 | + |
| 144 | +```cpp |
| 145 | +class Solution { |
| 146 | +public: |
| 147 | + int longestCommonPrefix(string s, string t) { |
| 148 | + int n = s.length(), m = t.length(); |
| 149 | + int i = 0, j = 0; |
| 150 | + bool rem = false; |
| 151 | + while (i < n && j < m) { |
| 152 | + if (s[i] != t[j]) { |
| 153 | + if (rem) { |
| 154 | + break; |
| 155 | + } |
| 156 | + rem = true; |
| 157 | + } else { |
| 158 | + ++j; |
| 159 | + } |
| 160 | + ++i; |
| 161 | + } |
| 162 | + return j; |
| 163 | + } |
| 164 | +}; |
| 165 | +``` |
| 166 | +
|
| 167 | +#### Go |
| 168 | +
|
| 169 | +```go |
| 170 | +func longestCommonPrefix(s string, t string) int { |
| 171 | + n, m := len(s), len(t) |
| 172 | + i, j := 0, 0 |
| 173 | + rem := false |
| 174 | + for i < n && j < m { |
| 175 | + if s[i] != t[j] { |
| 176 | + if rem { |
| 177 | + break |
| 178 | + } |
| 179 | + rem = true |
| 180 | + } else { |
| 181 | + j++ |
| 182 | + } |
| 183 | + i++ |
| 184 | + } |
| 185 | + return j |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +#### TypeScript |
| 190 | + |
| 191 | +```ts |
| 192 | +function longestCommonPrefix(s: string, t: string): number { |
| 193 | + const [n, m] = [s.length, t.length]; |
| 194 | + let [i, j] = [0, 0]; |
| 195 | + let rem: boolean = false; |
| 196 | + while (i < n && j < m) { |
| 197 | + if (s[i] !== t[j]) { |
| 198 | + if (rem) { |
| 199 | + break; |
| 200 | + } |
| 201 | + rem = true; |
| 202 | + } else { |
| 203 | + ++j; |
| 204 | + } |
| 205 | + ++i; |
| 206 | + } |
| 207 | + return j; |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +#### Rust |
| 212 | + |
| 213 | +```rust |
| 214 | +impl Solution { |
| 215 | + pub fn longest_common_prefix(s: String, t: String) -> i32 { |
| 216 | + let (n, m) = (s.len(), t.len()); |
| 217 | + let (mut i, mut j) = (0, 0); |
| 218 | + let mut rem = false; |
| 219 | + |
| 220 | + while i < n && j < m { |
| 221 | + if s.as_bytes()[i] != t.as_bytes()[j] { |
| 222 | + if rem { |
| 223 | + break; |
| 224 | + } |
| 225 | + rem = true; |
| 226 | + } else { |
| 227 | + j += 1; |
| 228 | + } |
| 229 | + i += 1; |
| 230 | + } |
| 231 | + |
| 232 | + j as i32 |
| 233 | + } |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +#### JavaScript |
| 238 | + |
| 239 | +```js |
| 240 | +/** |
| 241 | + * @param {string} s |
| 242 | + * @param {string} t |
| 243 | + * @return {number} |
| 244 | + */ |
| 245 | +var longestCommonPrefix = function (s, t) { |
| 246 | + const [n, m] = [s.length, t.length]; |
| 247 | + let [i, j] = [0, 0]; |
| 248 | + let rem = false; |
| 249 | + while (i < n && j < m) { |
| 250 | + if (s[i] !== t[j]) { |
| 251 | + if (rem) { |
| 252 | + break; |
| 253 | + } |
| 254 | + rem = true; |
| 255 | + } else { |
| 256 | + ++j; |
| 257 | + } |
| 258 | + ++i; |
| 259 | + } |
| 260 | + return j; |
| 261 | +}; |
| 262 | +``` |
| 263 | + |
| 264 | +<!-- tabs:end --> |
| 265 | + |
| 266 | +<!-- solution:end --> |
| 267 | + |
| 268 | +<!-- problem:end --> |
0 commit comments