From f0ca39cc749dcc970344d63e32289bbf5d877f6c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 18 Mar 2025 08:57:54 +0800 Subject: [PATCH] feat: update lc problems (#4259) --- .../3481.Apply Substitutions/README.md | 8 + .../3481.Apply Substitutions/README_EN.md | 8 + .../README.md | 5 + .../README_EN.md | 5 + .../3484.Design Spreadsheet/README.md | 6 + .../3484.Design Spreadsheet/README_EN.md | 6 + .../README.md | 4 + .../README_EN.md | 4 + .../3486.Longest Special Path II/README.md | 6 + .../3486.Longest Special Path II/README_EN.md | 6 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 3 + .../README_EN.md | 3 + .../3490.Count Beautiful Numbers/README.md | 2 + .../3490.Count Beautiful Numbers/README_EN.md | 2 + .../3491.Phone Number Prefix/README.md | 159 ++++++++++++++++++ .../3491.Phone Number Prefix/README_EN.md | 159 ++++++++++++++++++ .../3491.Phone Number Prefix/Solution.cpp | 18 ++ .../3491.Phone Number Prefix/Solution.go | 13 ++ .../3491.Phone Number Prefix/Solution.java | 14 ++ .../3491.Phone Number Prefix/Solution.py | 7 + .../3491.Phone Number Prefix/Solution.ts | 11 ++ solution/DATABASE_README.md | 2 +- solution/DATABASE_README_EN.md | 2 +- solution/README.md | 21 +-- solution/README_EN.md | 21 +-- 29 files changed, 489 insertions(+), 22 deletions(-) create mode 100644 solution/3400-3499/3491.Phone Number Prefix/README.md create mode 100644 solution/3400-3499/3491.Phone Number Prefix/README_EN.md create mode 100644 solution/3400-3499/3491.Phone Number Prefix/Solution.cpp create mode 100644 solution/3400-3499/3491.Phone Number Prefix/Solution.go create mode 100644 solution/3400-3499/3491.Phone Number Prefix/Solution.java create mode 100644 solution/3400-3499/3491.Phone Number Prefix/Solution.py create mode 100644 solution/3400-3499/3491.Phone Number Prefix/Solution.ts diff --git a/solution/3400-3499/3481.Apply Substitutions/README.md b/solution/3400-3499/3481.Apply Substitutions/README.md index 9b6bd1d93ffed..eebaf3ea31620 100644 --- a/solution/3400-3499/3481.Apply Substitutions/README.md +++ b/solution/3400-3499/3481.Apply Substitutions/README.md @@ -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: + - 深度优先搜索 + - 广度优先搜索 + - 图 + - 拓扑排序 + - 数组 + - 哈希表 + - 字符串 --- diff --git a/solution/3400-3499/3481.Apply Substitutions/README_EN.md b/solution/3400-3499/3481.Apply Substitutions/README_EN.md index 299e46c724f02..4b64b6cfa60e3 100644 --- a/solution/3400-3499/3481.Apply Substitutions/README_EN.md +++ b/solution/3400-3499/3481.Apply Substitutions/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md index 5310cc322db60..d7e199a10ad2b 100644 --- a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md @@ -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: + - 递归 + - 数组 + - 哈希表 + - 枚举 --- diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md index 4db4008b0ed56..285e5be7020bd 100644 --- a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3484.Design Spreadsheet/README.md b/solution/3400-3499/3484.Design Spreadsheet/README.md index b07c001613d8b..769a5862fd4a3 100644 --- a/solution/3400-3499/3484.Design Spreadsheet/README.md +++ b/solution/3400-3499/3484.Design Spreadsheet/README.md @@ -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: + - 设计 + - 数组 + - 哈希表 + - 字符串 + - 矩阵 --- diff --git a/solution/3400-3499/3484.Design Spreadsheet/README_EN.md b/solution/3400-3499/3484.Design Spreadsheet/README_EN.md index 59fb165db6f2c..3aee1a8087635 100644 --- a/solution/3400-3499/3484.Design Spreadsheet/README_EN.md +++ b/solution/3400-3499/3484.Design Spreadsheet/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md index 032cb56c7eeb8..e7fc56d1e6e20 100644 --- a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md +++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md @@ -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: + - 字典树 + - 数组 + - 字符串 --- diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md index 9ea7c70868cc4..1669f53620258 100644 --- a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md +++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3486.Longest Special Path II/README.md b/solution/3400-3499/3486.Longest Special Path II/README.md index d76d4f6913ec7..0711ee13b8479 100644 --- a/solution/3400-3499/3486.Longest Special Path II/README.md +++ b/solution/3400-3499/3486.Longest Special Path II/README.md @@ -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: + - 树 + - 深度优先搜索 + - 数组 + - 哈希表 + - 前缀和 --- diff --git a/solution/3400-3499/3486.Longest Special Path II/README_EN.md b/solution/3400-3499/3486.Longest Special Path II/README_EN.md index 653976137e22a..a9cc18031c9fc 100644 --- a/solution/3400-3499/3486.Longest Special Path II/README_EN.md +++ b/solution/3400-3499/3486.Longest Special Path II/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md index 44c6f26993998..568c8b3c242f1 100644 --- a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md @@ -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: + - 贪心 + - 数组 + - 哈希表 --- diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md index 3d7bedab4ec87..51038867d8f47 100644 --- a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3488.Closest Equal Element Queries/README.md b/solution/3400-3499/3488.Closest Equal Element Queries/README.md index 7764927be62c2..f3daecc1653d1 100644 --- a/solution/3400-3499/3488.Closest Equal Element Queries/README.md +++ b/solution/3400-3499/3488.Closest Equal Element Queries/README.md @@ -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: + - 数组 + - 哈希表 + - 二分查找 --- diff --git a/solution/3400-3499/3488.Closest Equal Element Queries/README_EN.md b/solution/3400-3499/3488.Closest Equal Element Queries/README_EN.md index aa395d42cc443..2b5a40b73b073 100644 --- a/solution/3400-3499/3488.Closest Equal Element Queries/README_EN.md +++ b/solution/3400-3499/3488.Closest Equal Element Queries/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3489.Zero Array Transformation IV/README.md b/solution/3400-3499/3489.Zero Array Transformation IV/README.md index 944812e7f5858..0e3e6f3d7ff0b 100644 --- a/solution/3400-3499/3489.Zero Array Transformation IV/README.md +++ b/solution/3400-3499/3489.Zero Array Transformation IV/README.md @@ -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: + - 数组 + - 动态规划 --- diff --git a/solution/3400-3499/3489.Zero Array Transformation IV/README_EN.md b/solution/3400-3499/3489.Zero Array Transformation IV/README_EN.md index f9b56b6ef218a..dbe1c15e719cc 100644 --- a/solution/3400-3499/3489.Zero Array Transformation IV/README_EN.md +++ b/solution/3400-3499/3489.Zero Array Transformation IV/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3490.Count Beautiful Numbers/README.md b/solution/3400-3499/3490.Count Beautiful Numbers/README.md index ee78b0668e74d..d3a150213b763 100644 --- a/solution/3400-3499/3490.Count Beautiful Numbers/README.md +++ b/solution/3400-3499/3490.Count Beautiful Numbers/README.md @@ -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: + - 动态规划 --- diff --git a/solution/3400-3499/3490.Count Beautiful Numbers/README_EN.md b/solution/3400-3499/3490.Count Beautiful Numbers/README_EN.md index 45024abdeb2cc..845fc70f01ab5 100644 --- a/solution/3400-3499/3490.Count Beautiful Numbers/README_EN.md +++ b/solution/3400-3499/3490.Count Beautiful Numbers/README_EN.md @@ -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 --- diff --git a/solution/3400-3499/3491.Phone Number Prefix/README.md b/solution/3400-3499/3491.Phone Number Prefix/README.md new file mode 100644 index 0000000000000..332ea87b28f41 --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/README.md @@ -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 +--- + + + +# [3491. Phone Number Prefix 🔒](https://leetcode.cn/problems/phone-number-prefix) + +[English Version](/solution/3400-3499/3491.Phone%20Number%20Prefix/README_EN.md) + +## 题目描述 + + + +

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

+ +

 

+

Example 1:

+ +
+

Input: numbers = ["1","2","4","3"]

+ +

Output: true

+ +

Explanation:

+ +

No number is a prefix of another number, so the output is true.

+
+ +

Example 2:

+ +
+

Input: numbers = ["001","007","15","00153"]

+ +

Output: false

+ +

Explanation:

+ +

The string "001" is a prefix of the string "00153". Thus, the output is false.

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:排序 + 前缀判断 + +我们可以先对 $\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}$ 数组中字符串的平均长度。 + + + +#### 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 + +class Solution { +public: + bool phonePrefix(vector& 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; +} +``` + + + + + + diff --git a/solution/3400-3499/3491.Phone Number Prefix/README_EN.md b/solution/3400-3499/3491.Phone Number Prefix/README_EN.md new file mode 100644 index 0000000000000..0d8a36ef5d97d --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/README_EN.md @@ -0,0 +1,159 @@ +--- +comments: true +difficulty: Easy +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3491.Phone%20Number%20Prefix/README_EN.md +--- + + + +# [3491. Phone Number Prefix 🔒](https://leetcode.com/problems/phone-number-prefix) + +[中文文档](/solution/3400-3499/3491.Phone%20Number%20Prefix/README.md) + +## Description + + + +

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

+ +

 

+

Example 1:

+ +
+

Input: numbers = ["1","2","4","3"]

+ +

Output: true

+ +

Explanation:

+ +

No number is a prefix of another number, so the output is true.

+
+ +

Example 2:

+ +
+

Input: numbers = ["001","007","15","00153"]

+ +

Output: false

+ +

Explanation:

+ +

The string "001" is a prefix of the string "00153". Thus, the output is false.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= numbers.length <= 50
  • +
  • 1 <= numbers[i].length <= 50
  • +
  • All numbers contain only digits '0' to '9'.
  • +
+ + + +## Solutions + + + +### Solution 1: Sorting + Prefix Checking + +We can first sort the array $\textit{numbers}$ based on the length of strings. Then, we iterate through each string $\textit{s}$ in the array and check if there is any previous string $\textit{t}$ that is a prefix of $\textit{s}$. If such a string exists, it means there is a string that is a prefix of another string, so we return $\textit{false}$. If we have checked all strings and haven't found any prefix relationships, we return $\textit{true}$. + +The time complexity is $O(n^2 \times m + n \times \log n)$, and the space complexity is $O(m + \log n)$, where $n$ is the length of the array $\textit{numbers}$, and $m$ is the average length of strings in the array $\textit{numbers}$. + + + +#### 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 + +class Solution { +public: + bool phonePrefix(vector& 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; +} +``` + + + + + + diff --git a/solution/3400-3499/3491.Phone Number Prefix/Solution.cpp b/solution/3400-3499/3491.Phone Number Prefix/Solution.cpp new file mode 100644 index 0000000000000..075735072858c --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/Solution.cpp @@ -0,0 +1,18 @@ +#include + +class Solution { +public: + bool phonePrefix(vector& 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; + } +}; diff --git a/solution/3400-3499/3491.Phone Number Prefix/Solution.go b/solution/3400-3499/3491.Phone Number Prefix/Solution.go new file mode 100644 index 0000000000000..b5c0de2e24979 --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/Solution.go @@ -0,0 +1,13 @@ +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 +} diff --git a/solution/3400-3499/3491.Phone Number Prefix/Solution.java b/solution/3400-3499/3491.Phone Number Prefix/Solution.java new file mode 100644 index 0000000000000..405b3dad00c18 --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/Solution.java @@ -0,0 +1,14 @@ +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; + } +} diff --git a/solution/3400-3499/3491.Phone Number Prefix/Solution.py b/solution/3400-3499/3491.Phone Number Prefix/Solution.py new file mode 100644 index 0000000000000..cf6769e6e18c1 --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/Solution.py @@ -0,0 +1,7 @@ +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 diff --git a/solution/3400-3499/3491.Phone Number Prefix/Solution.ts b/solution/3400-3499/3491.Phone Number Prefix/Solution.ts new file mode 100644 index 0000000000000..a621fc6348109 --- /dev/null +++ b/solution/3400-3499/3491.Phone Number Prefix/Solution.ts @@ -0,0 +1,11 @@ +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; +} diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 0a1bc9b19e728..8db7208bf0cb2 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -312,7 +312,7 @@ | 3451 | [查找无效的 IP 地址](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md) | `数据库` | 困难 | | | 3465 | [查找具有有效序列号的产品](/solution/3400-3499/3465.Find%20Products%20with%20Valid%20Serial%20Numbers/README.md) | `数据库` | 简单 | | | 3475 | [DNA 模式识别](/solution/3400-3499/3475.DNA%20Pattern%20Recognition/README.md) | | 中等 | | -| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | | 困难 | | +| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | `数据库` | 困难 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index ba28f5edfd872..fce0e86019893 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -310,7 +310,7 @@ Press Control + F(or Command + F on | 3451 | [Find Invalid IP Addresses](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md) | `Database` | Hard | | | 3465 | [Find Products with Valid Serial Numbers](/solution/3400-3499/3465.Find%20Products%20with%20Valid%20Serial%20Numbers/README_EN.md) | `Database` | Easy | | | 3475 | [DNA Pattern Recognition](/solution/3400-3499/3475.DNA%20Pattern%20Recognition/README_EN.md) | | Medium | | -| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | | Hard | | +| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | `Database` | Hard | | ## Copyright diff --git a/solution/README.md b/solution/README.md index bee2095c4a3d8..67fc5557ac800 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3491,16 +3491,17 @@ | 3478 | [选出和最大的 K 个元素](/solution/3400-3499/3478.Choose%20K%20Elements%20With%20Maximum%20Sum/README.md) | `数组`,`排序`,`堆(优先队列)` | 中等 | 第 440 场周赛 | | 3479 | [将水果装入篮子 III](/solution/3400-3499/3479.Fruits%20Into%20Baskets%20III/README.md) | `线段树`,`数组`,`二分查找`,`有序集合` | 中等 | 第 440 场周赛 | | 3480 | [删除一个冲突对后最大子数组数目](/solution/3400-3499/3480.Maximize%20Subarrays%20After%20Removing%20One%20Conflicting%20Pair/README.md) | `线段树`,`数组`,`枚举`,`前缀和` | 困难 | 第 440 场周赛 | -| 3481 | [Apply Substitutions](/solution/3400-3499/3481.Apply%20Substitutions/README.md) | | 中等 | 🔒 | -| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | | 困难 | | -| 3483 | [不同三位偶数的数目](/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README.md) | | 简单 | 第 152 场双周赛 | -| 3484 | [设计电子表格](/solution/3400-3499/3484.Design%20Spreadsheet/README.md) | | 中等 | 第 152 场双周赛 | -| 3485 | [删除元素后 K 个字符串的最长公共前缀](/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README.md) | | 困难 | 第 152 场双周赛 | -| 3486 | [最长特殊路径 II](/solution/3400-3499/3486.Longest%20Special%20Path%20II/README.md) | | 困难 | 第 152 场双周赛 | -| 3487 | [删除后的最大子数组元素和](/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README.md) | | 简单 | 第 441 场周赛 | -| 3488 | [距离最小相等元素查询](/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README.md) | | 中等 | 第 441 场周赛 | -| 3489 | [零数组变换 IV](/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README.md) | | 中等 | 第 441 场周赛 | -| 3490 | [统计美丽整数的数目](/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README.md) | | 困难 | 第 441 场周赛 | +| 3481 | [Apply Substitutions](/solution/3400-3499/3481.Apply%20Substitutions/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`拓扑排序`,`数组`,`哈希表`,`字符串` | 中等 | 🔒 | +| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | `数据库` | 困难 | | +| 3483 | [不同三位偶数的数目](/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README.md) | `递归`,`数组`,`哈希表`,`枚举` | 简单 | 第 152 场双周赛 | +| 3484 | [设计电子表格](/solution/3400-3499/3484.Design%20Spreadsheet/README.md) | `设计`,`数组`,`哈希表`,`字符串`,`矩阵` | 中等 | 第 152 场双周赛 | +| 3485 | [删除元素后 K 个字符串的最长公共前缀](/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README.md) | `字典树`,`数组`,`字符串` | 困难 | 第 152 场双周赛 | +| 3486 | [最长特殊路径 II](/solution/3400-3499/3486.Longest%20Special%20Path%20II/README.md) | `树`,`深度优先搜索`,`数组`,`哈希表`,`前缀和` | 困难 | 第 152 场双周赛 | +| 3487 | [删除后的最大子数组元素和](/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README.md) | `贪心`,`数组`,`哈希表` | 简单 | 第 441 场周赛 | +| 3488 | [距离最小相等元素查询](/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README.md) | `数组`,`哈希表`,`二分查找` | 中等 | 第 441 场周赛 | +| 3489 | [零数组变换 IV](/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README.md) | `数组`,`动态规划` | 中等 | 第 441 场周赛 | +| 3490 | [统计美丽整数的数目](/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README.md) | `动态规划` | 困难 | 第 441 场周赛 | +| 3491 | [Phone Number Prefix](/solution/3400-3499/3491.Phone%20Number%20Prefix/README.md) | | 简单 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index cc6a491138ec3..38007792a6658 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3489,16 +3489,17 @@ Press Control + F(or Command + F on | 3478 | [Choose K Elements With Maximum Sum](/solution/3400-3499/3478.Choose%20K%20Elements%20With%20Maximum%20Sum/README_EN.md) | `Array`,`Sorting`,`Heap (Priority Queue)` | Medium | Weekly Contest 440 | | 3479 | [Fruits Into Baskets III](/solution/3400-3499/3479.Fruits%20Into%20Baskets%20III/README_EN.md) | `Segment Tree`,`Array`,`Binary Search`,`Ordered Set` | Medium | Weekly Contest 440 | | 3480 | [Maximize Subarrays After Removing One Conflicting Pair](/solution/3400-3499/3480.Maximize%20Subarrays%20After%20Removing%20One%20Conflicting%20Pair/README_EN.md) | `Segment Tree`,`Array`,`Enumeration`,`Prefix Sum` | Hard | Weekly Contest 440 | -| 3481 | [Apply Substitutions](/solution/3400-3499/3481.Apply%20Substitutions/README_EN.md) | | Medium | 🔒 | -| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | | Hard | | -| 3483 | [Unique 3-Digit Even Numbers](/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README_EN.md) | | Easy | Biweekly Contest 152 | -| 3484 | [Design Spreadsheet](/solution/3400-3499/3484.Design%20Spreadsheet/README_EN.md) | | Medium | Biweekly Contest 152 | -| 3485 | [Longest Common Prefix of K Strings After Removal](/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README_EN.md) | | Hard | Biweekly Contest 152 | -| 3486 | [Longest Special Path II](/solution/3400-3499/3486.Longest%20Special%20Path%20II/README_EN.md) | | Hard | Biweekly Contest 152 | -| 3487 | [Maximum Unique Subarray Sum After Deletion](/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README_EN.md) | | Easy | Weekly Contest 441 | -| 3488 | [Closest Equal Element Queries](/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README_EN.md) | | Medium | Weekly Contest 441 | -| 3489 | [Zero Array Transformation IV](/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README_EN.md) | | Medium | Weekly Contest 441 | -| 3490 | [Count Beautiful Numbers](/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README_EN.md) | | Hard | Weekly Contest 441 | +| 3481 | [Apply Substitutions](/solution/3400-3499/3481.Apply%20Substitutions/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Topological Sort`,`Array`,`Hash Table`,`String` | Medium | 🔒 | +| 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | `Database` | Hard | | +| 3483 | [Unique 3-Digit Even Numbers](/solution/3400-3499/3483.Unique%203-Digit%20Even%20Numbers/README_EN.md) | `Recursion`,`Array`,`Hash Table`,`Enumeration` | Easy | Biweekly Contest 152 | +| 3484 | [Design Spreadsheet](/solution/3400-3499/3484.Design%20Spreadsheet/README_EN.md) | `Design`,`Array`,`Hash Table`,`String`,`Matrix` | Medium | Biweekly Contest 152 | +| 3485 | [Longest Common Prefix of K Strings After Removal](/solution/3400-3499/3485.Longest%20Common%20Prefix%20of%20K%20Strings%20After%20Removal/README_EN.md) | `Trie`,`Array`,`String` | Hard | Biweekly Contest 152 | +| 3486 | [Longest Special Path II](/solution/3400-3499/3486.Longest%20Special%20Path%20II/README_EN.md) | `Tree`,`Depth-First Search`,`Array`,`Hash Table`,`Prefix Sum` | Hard | Biweekly Contest 152 | +| 3487 | [Maximum Unique Subarray Sum After Deletion](/solution/3400-3499/3487.Maximum%20Unique%20Subarray%20Sum%20After%20Deletion/README_EN.md) | `Greedy`,`Array`,`Hash Table` | Easy | Weekly Contest 441 | +| 3488 | [Closest Equal Element Queries](/solution/3400-3499/3488.Closest%20Equal%20Element%20Queries/README_EN.md) | `Array`,`Hash Table`,`Binary Search` | Medium | Weekly Contest 441 | +| 3489 | [Zero Array Transformation IV](/solution/3400-3499/3489.Zero%20Array%20Transformation%20IV/README_EN.md) | `Array`,`Dynamic Programming` | Medium | Weekly Contest 441 | +| 3490 | [Count Beautiful Numbers](/solution/3400-3499/3490.Count%20Beautiful%20Numbers/README_EN.md) | `Dynamic Programming` | Hard | Weekly Contest 441 | +| 3491 | [Phone Number Prefix](/solution/3400-3499/3491.Phone%20Number%20Prefix/README_EN.md) | | Easy | 🔒 | ## Copyright