Skip to content

[pull] main from doocs:main #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions solution/3400-3499/3481.Apply Substitutions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Apply%20Substitutions/README.md
tags:
- 深度优先搜索
- 广度优先搜索
- 图
- 拓扑排序
- 数组
- 哈希表
- 字符串
---

<!-- problem:start -->
Expand Down
8 changes: 8 additions & 0 deletions solution/3400-3499/3481.Apply Substitutions/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Apply%20Substitutions/README_EN.md
tags:
- Depth-First Search
- Breadth-First Search
- Graph
- Topological Sort
- Array
- Hash Table
- String
---

<!-- problem:start -->
Expand Down
5 changes: 5 additions & 0 deletions solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README.md
tags:
- 递归
- 数组
- 哈希表
- 枚举
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README_EN.md
tags:
- Recursion
- Array
- Hash Table
- Enumeration
---

<!-- problem:start -->
Expand Down
6 changes: 6 additions & 0 deletions solution/3400-3499/3484.Design Spreadsheet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3484.Design%20Spreadsheet/README.md
tags:
- 设计
- 数组
- 哈希表
- 字符串
- 矩阵
---

<!-- problem:start -->
Expand Down
6 changes: 6 additions & 0 deletions solution/3400-3499/3484.Design Spreadsheet/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3484.Design%20Spreadsheet/README_EN.md
tags:
- Design
- Array
- Hash Table
- String
- Matrix
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README.md
tags:
- 字典树
- 数组
- 字符串
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README_EN.md
tags:
- Trie
- Array
- String
---

<!-- problem:start -->
Expand Down
6 changes: 6 additions & 0 deletions solution/3400-3499/3486.Longest Special Path II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3486.Longest%20Special%20Path%20II/README.md
tags:
- 树
- 深度优先搜索
- 数组
- 哈希表
- 前缀和
---

<!-- problem:start -->
Expand Down
6 changes: 6 additions & 0 deletions solution/3400-3499/3486.Longest Special Path II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3486.Longest%20Special%20Path%20II/README_EN.md
tags:
- Tree
- Depth-First Search
- Array
- Hash Table
- Prefix Sum
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README.md
tags:
- 贪心
- 数组
- 哈希表
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README_EN.md
tags:
- Greedy
- Array
- Hash Table
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README.md
tags:
- 数组
- 哈希表
- 二分查找
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README_EN.md
tags:
- Array
- Hash Table
- Binary Search
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README.md
tags:
- 数组
- 动态规划
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README_EN.md
tags:
- Array
- Dynamic Programming
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3400-3499/3490.Count Beautiful Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README.md
tags:
- 动态规划
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3400-3499/3490.Count Beautiful Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README_EN.md
tags:
- Dynamic Programming
---

<!-- problem:start -->
Expand Down
159 changes: 159 additions & 0 deletions solution/3400-3499/3491.Phone Number Prefix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3491.Phone%20Number%20Prefix/README.md
---

<!-- problem:start -->

# [3491. Phone Number Prefix 🔒](https://leetcode.cn/problems/phone-number-prefix)

[English Version](/solution/3400-3499/3491.Phone%20Number%20Prefix/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given a string array <code>numbers</code> that represents phone numbers. Return <code>true</code> if no phone number is a prefix of any other phone number; otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">numbers = [&quot;1&quot;,&quot;2&quot;,&quot;4&quot;,&quot;3&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p>No number is a prefix of another number, so the output is <code>true</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">numbers = [&quot;001&quot;,&quot;007&quot;,&quot;15&quot;,&quot;00153&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>

<p><strong>Explanation:</strong></p>

<p>The string <code>&quot;001&quot;</code> is a prefix of the string <code>&quot;00153&quot;</code>. Thus, the output is <code>false</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= numbers.length &lt;= 50</code></li>
<li><code>1 &lt;= numbers[i].length &lt;= 50</code></li>
<li>All numbers contain only digits <code>&#39;0&#39;</code> to <code>&#39;9&#39;</code>.</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:排序 + 前缀判断

我们可以先对 $\textit{numbers}$ 数组按照字符串长度进行排序,然后遍历数组中的每一个字符串 $\textit{s}$,判断此前是否有字符串 $\textit{t}$ 是 $\textit{s}$ 的前缀,如果有,说明存在一个字符串是另一个字符串的前缀,返回 $\textit{false}$。如果遍历完所有字符串都没有找到前缀关系,返回 $\textit{true}$。

时间复杂度 $(n^2 \times m + n \times \log n)$,空间复杂度 $(m + \log n)$,其中 $n$ 是 $\textit{numbers}$ 数组的长度,而 $m$ 是 $\textit{numbers}$ 数组中字符串的平均长度。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def phonePrefix(self, numbers: List[str]) -> bool:
numbers.sort(key=len)
for i, s in enumerate(numbers):
if any(s.startswith(t) for t in numbers[:i]):
return False
return True
```

#### Java

```java
class Solution {
public boolean phonePrefix(String[] numbers) {
Arrays.sort(numbers, (a, b) -> Integer.compare(a.length(), b.length()));
for (int i = 0; i < numbers.length; i++) {
String s = numbers[i];
for (int j = 0; j < i; j++) {
if (s.startsWith(numbers[j])) {
return false;
}
}
}
return true;
}
}
```

#### C++

```cpp
#include <ranges>

class Solution {
public:
bool phonePrefix(vector<string>& numbers) {
ranges::sort(numbers, [](const string& a, const string& b) {
return a.size() < b.size();
});
for (int i = 0; i < numbers.size(); i++) {
if (ranges::any_of(numbers | views::take(i), [&](const string& t) {
return numbers[i].starts_with(t);
})) {
return false;
}
}
return true;
}
};
```

#### Go

```go
func phonePrefix(numbers []string) bool {
sort.Slice(numbers, func(i, j int) bool {
return len(numbers[i]) < len(numbers[j])
})
for i, s := range numbers {
for _, t := range numbers[:i] {
if strings.HasPrefix(s, t) {
return false
}
}
}
return true
}
```

#### TypeScript

```ts
function phonePrefix(numbers: string[]): boolean {
numbers.sort((a, b) => a.length - b.length);
for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < i; j++) {
if (numbers[i].startsWith(numbers[j])) {
return false;
}
}
}
return true;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading