From 2dbb8df848b73ad3fe315073f2b0076f615f0055 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 13 Mar 2024 09:07:58 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3076 No.3076.Shortest Uncommon Substring in an Array --- .../README.md | 2 +- .../README_EN.md | 6 +- .../README.md | 129 +++++++++++++++++- .../README_EN.md | 129 +++++++++++++++++- .../Solution.cpp | 27 ++++ .../Solution.go | 24 ++++ .../Solution.java | 27 ++++ .../Solution.py | 14 ++ .../Solution.ts | 24 ++++ 9 files changed, 372 insertions(+), 10 deletions(-) create mode 100644 solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.cpp create mode 100644 solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.go create mode 100644 solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.java create mode 100644 solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.py create mode 100644 solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.ts diff --git a/solution/2600-2699/2674.Split a Circular Linked List/README.md b/solution/2600-2699/2674.Split a Circular Linked List/README.md index 30d9acfa2dab3..df9ae5a38e8fa 100644 --- a/solution/2600-2699/2674.Split a Circular Linked List/README.md +++ b/solution/2600-2699/2674.Split a Circular Linked List/README.md @@ -46,7 +46,7 @@ ### 方法一:快慢指针 -我们定义两个指针 $a$ 和 $b$,初始时都指向链表的头节点。每次迭代时,$a$ 指针向前移动一步,$b$ 指针向前移动两步,直到 $b$ 指针到达链表的末尾。此时,$a$ 指针指向链表节点数的一半,我们将链表从 $a$ 指针处断开,即可得到两个链表的头节点。 +我们定义两个指针 $a$ 和 $b$,初始时都指向链表的头节点。每次迭代时,指针 $a$ 向前移动一步,指针 $b$ 向前移动两步,直到指针 $b$ 到达链表的末尾。此时,指针 $a$ 指向链表节点数的一半,我们将链表从指针 $a$ 处断开,即可得到两个链表的头节点。 时间复杂度 $O(n)$,其中 $n$ 是链表的长度。需要遍历链表一次。空间复杂度 $O(1)$。 diff --git a/solution/2600-2699/2674.Split a Circular Linked List/README_EN.md b/solution/2600-2699/2674.Split a Circular Linked List/README_EN.md index fb0aa01fe539d..750ffcccd3eab 100644 --- a/solution/2600-2699/2674.Split a Circular Linked List/README_EN.md +++ b/solution/2600-2699/2674.Split a Circular Linked List/README_EN.md @@ -40,7 +40,11 @@ ## Solutions -### Solution 1 +### Solution 1: Fast and Slow Pointers + +We define two pointers $a$ and $b$, both initially pointing to the head of the linked list. Each iteration, pointer $a$ moves forward one step, and pointer $b$ moves forward two steps, until pointer $b$ reaches the end of the linked list. At this point, pointer $a$ points to half of the linked list nodes, and we break the linked list from pointer $a$, thus obtaining the head nodes of the two linked lists. + +The time complexity is $O(n)$, where $n$ is the length of the linked list. It requires one traversal of the linked list. The space complexity is $O(1)$. diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README.md b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README.md index f6ac658d53ae1..741ff20212c3c 100644 --- a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README.md +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README.md @@ -56,24 +56,145 @@ ## 解法 -### 方法一 +### 方法一:枚举 + +我们注意到数据规模很小,所以可以直接枚举每个字符串的所有子串,然后判断是否是其他字符串的子串。 + +具体地,我们先枚举每个字符串 `arr[i]`,然后从小到大枚举每个子串的长度 $j$,然后枚举每个子串的起始位置 $l$,可以得到当前子串为 `sub = arr[i][l:l+j]`,然后判断 `sub` 是否是其他字符串的子串,如果是的话,就跳过当前子串,否则更新答案。 + +时间复杂度 $O(n^2 \times m^4)$,空间复杂度 $O(m)$。其中 $n$ 是字符串数组 `arr` 的长度,而 $m$ 是字符串的最大长度,本题中 $m \le 20$。 ```python - +class Solution: + def shortestSubstrings(self, arr: List[str]) -> List[str]: + ans = [""] * len(arr) + for i, s in enumerate(arr): + m = len(s) + for j in range(1, m + 1): + for l in range(m - j + 1): + sub = s[l : l + j] + if not ans[i] or ans[i] > sub: + if all(k == i or sub not in t for k, t in enumerate(arr)): + ans[i] = sub + if ans[i]: + break + return ans ``` ```java - +class Solution { + public String[] shortestSubstrings(String[] arr) { + int n = arr.length; + String[] ans = new String[n]; + Arrays.fill(ans, ""); + for (int i = 0; i < n; ++i) { + int m = arr[i].length(); + for (int j = 1; j <= m && ans[i].isEmpty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + String sub = arr[i].substring(l, l + j); + if (ans[i].isEmpty() || sub.compareTo(ans[i]) < 0) { + boolean ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].contains(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + vector shortestSubstrings(vector& arr) { + int n = arr.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + int m = arr[i].size(); + for (int j = 1; j <= m && ans[i].empty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + string sub = arr[i].substr(l, j); + if (ans[i].empty() || sub < ans[i]) { + bool ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].find(sub) != string::npos) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +}; ``` ```go +func shortestSubstrings(arr []string) []string { + ans := make([]string, len(arr)) + for i, s := range arr { + m := len(s) + for j := 1; j <= m && len(ans[i]) == 0; j++ { + for l := 0; l <= m-j; l++ { + sub := s[l : l+j] + if len(ans[i]) == 0 || ans[i] > sub { + ok := true + for k, t := range arr { + if k != i && strings.Contains(t, sub) { + ok = false + break + } + } + if ok { + ans[i] = sub + } + } + } + } + } + return ans +} +``` +```ts +function shortestSubstrings(arr: string[]): string[] { + const n: number = arr.length; + const ans: string[] = Array(n).fill(''); + for (let i = 0; i < n; ++i) { + const m: number = arr[i].length; + for (let j = 1; j <= m && ans[i] === ''; ++j) { + for (let l = 0; l <= m - j; ++l) { + const sub: string = arr[i].slice(l, l + j); + if (ans[i] === '' || sub.localeCompare(ans[i]) < 0) { + let ok: boolean = true; + for (let k = 0; k < n && ok; ++k) { + if (k !== i && arr[k].includes(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README_EN.md b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README_EN.md index 0ba3803dbf33f..abef5950aa3d1 100644 --- a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README_EN.md +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README_EN.md @@ -52,24 +52,145 @@ ## Solutions -### Solution 1 +### Solution 1: Enumeration + +Given the small data scale, we can directly enumerate all substrings of each string and then determine whether it is a substring of other strings. + +Specifically, we first enumerate each string `arr[i]`, then enumerate the length $j$ of each substring from small to large, and then enumerate the starting position $l$ of each substring. We can get the current substring as `sub = arr[i][l:l+j]`. Then we determine whether `sub` is a substring of other strings. If it is, we skip the current substring; otherwise, we update the answer. + +The time complexity is $O(n^2 \times m^4)$, and the space complexity is $O(m)$. Where $n$ is the length of the string array `arr`, and $m$ is the maximum length of the string. In this problem, $m \le 20$. ```python - +class Solution: + def shortestSubstrings(self, arr: List[str]) -> List[str]: + ans = [""] * len(arr) + for i, s in enumerate(arr): + m = len(s) + for j in range(1, m + 1): + for l in range(m - j + 1): + sub = s[l : l + j] + if not ans[i] or ans[i] > sub: + if all(k == i or sub not in t for k, t in enumerate(arr)): + ans[i] = sub + if ans[i]: + break + return ans ``` ```java - +class Solution { + public String[] shortestSubstrings(String[] arr) { + int n = arr.length; + String[] ans = new String[n]; + Arrays.fill(ans, ""); + for (int i = 0; i < n; ++i) { + int m = arr[i].length(); + for (int j = 1; j <= m && ans[i].isEmpty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + String sub = arr[i].substring(l, l + j); + if (ans[i].isEmpty() || sub.compareTo(ans[i]) < 0) { + boolean ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].contains(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + vector shortestSubstrings(vector& arr) { + int n = arr.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + int m = arr[i].size(); + for (int j = 1; j <= m && ans[i].empty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + string sub = arr[i].substr(l, j); + if (ans[i].empty() || sub < ans[i]) { + bool ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].find(sub) != string::npos) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +}; ``` ```go +func shortestSubstrings(arr []string) []string { + ans := make([]string, len(arr)) + for i, s := range arr { + m := len(s) + for j := 1; j <= m && len(ans[i]) == 0; j++ { + for l := 0; l <= m-j; l++ { + sub := s[l : l+j] + if len(ans[i]) == 0 || ans[i] > sub { + ok := true + for k, t := range arr { + if k != i && strings.Contains(t, sub) { + ok = false + break + } + } + if ok { + ans[i] = sub + } + } + } + } + } + return ans +} +``` +```ts +function shortestSubstrings(arr: string[]): string[] { + const n: number = arr.length; + const ans: string[] = Array(n).fill(''); + for (let i = 0; i < n; ++i) { + const m: number = arr[i].length; + for (let j = 1; j <= m && ans[i] === ''; ++j) { + for (let l = 0; l <= m - j; ++l) { + const sub: string = arr[i].slice(l, l + j); + if (ans[i] === '' || sub.localeCompare(ans[i]) < 0) { + let ok: boolean = true; + for (let k = 0; k < n && ok; ++k) { + if (k !== i && arr[k].includes(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.cpp b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.cpp new file mode 100644 index 0000000000000..d158095767060 --- /dev/null +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector shortestSubstrings(vector& arr) { + int n = arr.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + int m = arr[i].size(); + for (int j = 1; j <= m && ans[i].empty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + string sub = arr[i].substr(l, j); + if (ans[i].empty() || sub < ans[i]) { + bool ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].find(sub) != string::npos) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.go b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.go new file mode 100644 index 0000000000000..896c5b928f46c --- /dev/null +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.go @@ -0,0 +1,24 @@ +func shortestSubstrings(arr []string) []string { + ans := make([]string, len(arr)) + for i, s := range arr { + m := len(s) + for j := 1; j <= m && len(ans[i]) == 0; j++ { + for l := 0; l <= m-j; l++ { + sub := s[l : l+j] + if len(ans[i]) == 0 || ans[i] > sub { + ok := true + for k, t := range arr { + if k != i && strings.Contains(t, sub) { + ok = false + break + } + } + if ok { + ans[i] = sub + } + } + } + } + } + return ans +} \ No newline at end of file diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.java b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.java new file mode 100644 index 0000000000000..3b78eccb28068 --- /dev/null +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.java @@ -0,0 +1,27 @@ +class Solution { + public String[] shortestSubstrings(String[] arr) { + int n = arr.length; + String[] ans = new String[n]; + Arrays.fill(ans, ""); + for (int i = 0; i < n; ++i) { + int m = arr[i].length(); + for (int j = 1; j <= m && ans[i].isEmpty(); ++j) { + for (int l = 0; l <= m - j; ++l) { + String sub = arr[i].substring(l, l + j); + if (ans[i].isEmpty() || sub.compareTo(ans[i]) < 0) { + boolean ok = true; + for (int k = 0; k < n && ok; ++k) { + if (k != i && arr[k].contains(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.py b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.py new file mode 100644 index 0000000000000..9ee3f258b3eda --- /dev/null +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def shortestSubstrings(self, arr: List[str]) -> List[str]: + ans = [""] * len(arr) + for i, s in enumerate(arr): + m = len(s) + for j in range(1, m + 1): + for l in range(m - j + 1): + sub = s[l : l + j] + if not ans[i] or ans[i] > sub: + if all(k == i or sub not in t for k, t in enumerate(arr)): + ans[i] = sub + if ans[i]: + break + return ans diff --git a/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.ts b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.ts new file mode 100644 index 0000000000000..5121ef88898b8 --- /dev/null +++ b/solution/3000-3099/3076.Shortest Uncommon Substring in an Array/Solution.ts @@ -0,0 +1,24 @@ +function shortestSubstrings(arr: string[]): string[] { + const n: number = arr.length; + const ans: string[] = Array(n).fill(''); + for (let i = 0; i < n; ++i) { + const m: number = arr[i].length; + for (let j = 1; j <= m && ans[i] === ''; ++j) { + for (let l = 0; l <= m - j; ++l) { + const sub: string = arr[i].slice(l, l + j); + if (ans[i] === '' || sub.localeCompare(ans[i]) < 0) { + let ok: boolean = true; + for (let k = 0; k < n && ok; ++k) { + if (k !== i && arr[k].includes(sub)) { + ok = false; + } + } + if (ok) { + ans[i] = sub; + } + } + } + } + } + return ans; +}