From 8e962722ac004115a905f4f7bb2dec24e8706813 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 17 Jun 2024 09:09:37 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.522 No.0522.Longest Uncommon Subsequence II --- .../README.md | 150 ++++++++++------- .../README_EN.md | 152 +++++++++++------- .../Solution.cpp | 29 ++-- .../Solution.go | 31 ++-- .../Solution.java | 27 ++-- .../Solution.py | 27 ++-- .../Solution.ts | 25 +++ 7 files changed, 264 insertions(+), 177 deletions(-) create mode 100644 solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.ts diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md b/solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md index 4a26eb738656e..c4e48ae4454f2 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md @@ -64,9 +64,11 @@ tags: ### 方法一:判断子序列 -判断是否独有,只需要取字符串 $s$ 本身,与其他字符串比较即可。题目可以转化为:获取**非其他字符串子序列**的字符串的最大长度。若不存在,返回 -1。 +我们定义一个函数 $check(s, t)$,用于判断字符串 $s$ 是否是字符串 $t$ 的子序列。我们可以使用双指针的方法,初始化两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 和字符串 $t$ 的开头,然后不断移动指针 $j$,如果 $s[i]$ 和 $t[j]$ 相等,则移动指针 $i$,最后判断 $i$ 是否等于 $s$ 的长度即可。若 $i$ 等于 $s$ 的长度,则说明 $s$ 是 $t$ 的子序列。 -其中,$check(a,b)$ 用于判断字符串 $b$ 是否为字符串 $a$ 的子序列。 +判断字符串 $s$ 是否独有,只需要取字符串 $s$ 本身,与字符串列表的其他字符串比较即可。如果存在 $s$ 是其他字符串的子序列,则 $s$ 不是独有的。否则,字符串 $s$ 是独有的。我们取所有独有字符串中长度最长的字符串即可。 + +时间复杂度 $O(n^2 \times m)$,其中 $n$ 是字符串列表的长度,而 $m$ 是字符串的平均长度。空间复杂度 $O(1)$。 @@ -75,26 +77,21 @@ tags: ```python class Solution: def findLUSlength(self, strs: List[str]) -> int: - def check(a, b): + def check(s: str, t: str): i = j = 0 - while i < len(a) and j < len(b): - if a[i] == b[j]: - j += 1 - i += 1 - return j == len(b) + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + return i == len(s) - n = len(strs) ans = -1 - - for i in range(n): - j = 0 - while j < n: - if i == j or not check(strs[j], strs[i]): - j += 1 - else: + for i, s in enumerate(strs): + for j, t in enumerate(strs): + if i != j and check(s, t): break - if j == n: - ans = max(ans, len(strs[i])) + else: + ans = max(ans, len(s)) return ans ``` @@ -104,30 +101,29 @@ class Solution: class Solution { public int findLUSlength(String[] strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.length; i < n; ++i) { + int n = strs.length; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].length(); for (j = 0; j < n; ++j) { - if (i == j) { - continue; - } - if (check(strs[j], strs[i])) { + if (i != j && check(strs[i], strs[j])) { + x = -1; break; } } - if (j == n) { - ans = Math.max(ans, strs[i].length()); - } + ans = Math.max(ans, x); } return ans; } - private boolean check(String a, String b) { - int j = 0; - for (int i = 0; i < a.length() && j < b.length(); ++i) { - if (a.charAt(i) == b.charAt(j)) { - ++j; + private boolean check(String s, String t) { + int m = s.length(), n = t.length(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s.charAt(i) == t.charAt(j)) { + ++i; } } - return j == b.length(); + return i == m; } } ``` @@ -139,22 +135,29 @@ class Solution { public: int findLUSlength(vector& strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.size(); i < n; ++i) { + int n = strs.size(); + auto check = [&](const string& s, const string& t) { + int m = s.size(), n = t.size(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s[i] == t[j]) { + ++i; + } + } + return i == m; + }; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].size(); for (j = 0; j < n; ++j) { - if (i == j) continue; - if (check(strs[j], strs[i])) break; + if (i != j && check(strs[i], strs[j])) { + x = -1; + break; + } } - if (j == n) ans = max(ans, (int) strs[i].size()); + ans = max(ans, x); } return ans; } - - bool check(string a, string b) { - int j = 0; - for (int i = 0; i < a.size() && j < b.size(); ++i) - if (a[i] == b[j]) ++j; - return j == b.size(); - } }; ``` @@ -162,34 +165,61 @@ public: ```go func findLUSlength(strs []string) int { - check := func(a, b string) bool { - j := 0 - for i := 0; i < len(a) && j < len(b); i++ { - if a[i] == b[j] { - j++ + ans := -1 + check := func(s, t string) bool { + m, n := len(s), len(t) + i := 0 + for j := 0; i < m && j < n; j++ { + if s[i] == t[j] { + i++ } } - return j == len(b) + return i == m } - - ans := -1 - for i, j, n := 0, 0, len(strs); i < n; i++ { - for j = 0; j < n; j++ { - if i == j { - continue - } - if check(strs[j], strs[i]) { + for i, s := range strs { + x := len(s) + for j, t := range strs { + if i != j && check(s, t) { + x = -1 break } } - if j == n && ans < len(strs[i]) { - ans = len(strs[i]) - } + ans = max(ans, x) } return ans } ``` +#### TypeScript + +```ts +function findLUSlength(strs: string[]): number { + const n = strs.length; + let ans = -1; + const check = (s: string, t: string): boolean => { + const [m, n] = [s.length, t.length]; + let i = 0; + for (let j = 0; i < m && j < n; ++j) { + if (s[i] === t[j]) { + ++i; + } + } + return i === m; + }; + for (let i = 0; i < n; ++i) { + let x = strs[i].length; + for (let j = 0; j < n; ++j) { + if (i !== j && check(strs[i], strs[j])) { + x = -1; + break; + } + } + ans = Math.max(ans, x); + } + return ans; +} +``` + diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/README_EN.md b/solution/0500-0599/0522.Longest Uncommon Subsequence II/README_EN.md index 1ac3630d2c25f..5aacf123f4d62 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/README_EN.md +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/README_EN.md @@ -53,7 +53,13 @@ tags: -### Solution 1 +### Solution 1: Subsequence Judgment + +We define a function $check(s, t)$ to determine whether string $s$ is a subsequence of string $t$. We can use a two-pointer approach, initializing two pointers $i$ and $j$ to point to the beginning of strings $s$ and $t$ respectively, then continuously move pointer $j$. If $s[i]$ equals $t[j]$, then move pointer $i$. Finally, check if $i$ equals the length of $s$. If $i$ equals the length of $s$, it means $s$ is a subsequence of $t$. + +To determine if string $s$ is unique, we only need to take string $s$ itself and compare it with other strings in the list. If there exists a string for which $s$ is a subsequence, then $s$ is not unique. Otherwise, string $s$ is unique. We take the longest string among all unique strings. + +The time complexity is $O(n^2 \times m)$, where $n$ is the length of the list of strings, and $m$ is the average length of the strings. The space complexity is $O(1)$. @@ -62,26 +68,21 @@ tags: ```python class Solution: def findLUSlength(self, strs: List[str]) -> int: - def check(a, b): + def check(s: str, t: str): i = j = 0 - while i < len(a) and j < len(b): - if a[i] == b[j]: - j += 1 - i += 1 - return j == len(b) + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + return i == len(s) - n = len(strs) ans = -1 - - for i in range(n): - j = 0 - while j < n: - if i == j or not check(strs[j], strs[i]): - j += 1 - else: + for i, s in enumerate(strs): + for j, t in enumerate(strs): + if i != j and check(s, t): break - if j == n: - ans = max(ans, len(strs[i])) + else: + ans = max(ans, len(s)) return ans ``` @@ -91,30 +92,29 @@ class Solution: class Solution { public int findLUSlength(String[] strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.length; i < n; ++i) { + int n = strs.length; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].length(); for (j = 0; j < n; ++j) { - if (i == j) { - continue; - } - if (check(strs[j], strs[i])) { + if (i != j && check(strs[i], strs[j])) { + x = -1; break; } } - if (j == n) { - ans = Math.max(ans, strs[i].length()); - } + ans = Math.max(ans, x); } return ans; } - private boolean check(String a, String b) { - int j = 0; - for (int i = 0; i < a.length() && j < b.length(); ++i) { - if (a.charAt(i) == b.charAt(j)) { - ++j; + private boolean check(String s, String t) { + int m = s.length(), n = t.length(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s.charAt(i) == t.charAt(j)) { + ++i; } } - return j == b.length(); + return i == m; } } ``` @@ -126,22 +126,29 @@ class Solution { public: int findLUSlength(vector& strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.size(); i < n; ++i) { + int n = strs.size(); + auto check = [&](const string& s, const string& t) { + int m = s.size(), n = t.size(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s[i] == t[j]) { + ++i; + } + } + return i == m; + }; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].size(); for (j = 0; j < n; ++j) { - if (i == j) continue; - if (check(strs[j], strs[i])) break; + if (i != j && check(strs[i], strs[j])) { + x = -1; + break; + } } - if (j == n) ans = max(ans, (int) strs[i].size()); + ans = max(ans, x); } return ans; } - - bool check(string a, string b) { - int j = 0; - for (int i = 0; i < a.size() && j < b.size(); ++i) - if (a[i] == b[j]) ++j; - return j == b.size(); - } }; ``` @@ -149,34 +156,61 @@ public: ```go func findLUSlength(strs []string) int { - check := func(a, b string) bool { - j := 0 - for i := 0; i < len(a) && j < len(b); i++ { - if a[i] == b[j] { - j++ + ans := -1 + check := func(s, t string) bool { + m, n := len(s), len(t) + i := 0 + for j := 0; i < m && j < n; j++ { + if s[i] == t[j] { + i++ } } - return j == len(b) + return i == m } - - ans := -1 - for i, j, n := 0, 0, len(strs); i < n; i++ { - for j = 0; j < n; j++ { - if i == j { - continue - } - if check(strs[j], strs[i]) { + for i, s := range strs { + x := len(s) + for j, t := range strs { + if i != j && check(s, t) { + x = -1 break } } - if j == n && ans < len(strs[i]) { - ans = len(strs[i]) - } + ans = max(ans, x) } return ans } ``` +#### TypeScript + +```ts +function findLUSlength(strs: string[]): number { + const n = strs.length; + let ans = -1; + const check = (s: string, t: string): boolean => { + const [m, n] = [s.length, t.length]; + let i = 0; + for (let j = 0; i < m && j < n; ++j) { + if (s[i] === t[j]) { + ++i; + } + } + return i === m; + }; + for (let i = 0; i < n; ++i) { + let x = strs[i].length; + for (let j = 0; j < n; ++j) { + if (i !== j && check(strs[i], strs[j])) { + x = -1; + break; + } + } + ans = Math.max(ans, x); + } + return ans; +} +``` + diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp index 75656438988a2..404238ff81dc2 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp @@ -2,20 +2,27 @@ class Solution { public: int findLUSlength(vector& strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.size(); i < n; ++i) { + int n = strs.size(); + auto check = [&](const string& s, const string& t) { + int m = s.size(), n = t.size(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s[i] == t[j]) { + ++i; + } + } + return i == m; + }; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].size(); for (j = 0; j < n; ++j) { - if (i == j) continue; - if (check(strs[j], strs[i])) break; + if (i != j && check(strs[i], strs[j])) { + x = -1; + break; + } } - if (j == n) ans = max(ans, (int) strs[i].size()); + ans = max(ans, x); } return ans; } - - bool check(string a, string b) { - int j = 0; - for (int i = 0; i < a.size() && j < b.size(); ++i) - if (a[i] == b[j]) ++j; - return j == b.size(); - } }; \ No newline at end of file diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.go b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.go index bc03c436b62a4..a391e5a7596c9 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.go +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.go @@ -1,27 +1,24 @@ func findLUSlength(strs []string) int { - check := func(a, b string) bool { - j := 0 - for i := 0; i < len(a) && j < len(b); i++ { - if a[i] == b[j] { - j++ + ans := -1 + check := func(s, t string) bool { + m, n := len(s), len(t) + i := 0 + for j := 0; i < m && j < n; j++ { + if s[i] == t[j] { + i++ } } - return j == len(b) + return i == m } - - ans := -1 - for i, j, n := 0, 0, len(strs); i < n; i++ { - for j = 0; j < n; j++ { - if i == j { - continue - } - if check(strs[j], strs[i]) { + for i, s := range strs { + x := len(s) + for j, t := range strs { + if i != j && check(s, t) { + x = -1 break } } - if j == n && ans < len(strs[i]) { - ans = len(strs[i]) - } + ans = max(ans, x) } return ans } \ No newline at end of file diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java index a484e7f1eed51..09348b09a3aff 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.java @@ -1,29 +1,28 @@ class Solution { public int findLUSlength(String[] strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.length; i < n; ++i) { + int n = strs.length; + for (int i = 0, j; i < n; ++i) { + int x = strs[i].length(); for (j = 0; j < n; ++j) { - if (i == j) { - continue; - } - if (check(strs[j], strs[i])) { + if (i != j && check(strs[i], strs[j])) { + x = -1; break; } } - if (j == n) { - ans = Math.max(ans, strs[i].length()); - } + ans = Math.max(ans, x); } return ans; } - private boolean check(String a, String b) { - int j = 0; - for (int i = 0; i < a.length() && j < b.length(); ++i) { - if (a.charAt(i) == b.charAt(j)) { - ++j; + private boolean check(String s, String t) { + int m = s.length(), n = t.length(); + int i = 0; + for (int j = 0; i < m && j < n; ++j) { + if (s.charAt(i) == t.charAt(j)) { + ++i; } } - return j == b.length(); + return i == m; } } \ No newline at end of file diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.py b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.py index 583af90e75ff2..7569c144505d5 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.py +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.py @@ -1,23 +1,18 @@ class Solution: def findLUSlength(self, strs: List[str]) -> int: - def check(a, b): + def check(s: str, t: str): i = j = 0 - while i < len(a) and j < len(b): - if a[i] == b[j]: - j += 1 - i += 1 - return j == len(b) + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + return i == len(s) - n = len(strs) ans = -1 - - for i in range(n): - j = 0 - while j < n: - if i == j or not check(strs[j], strs[i]): - j += 1 - else: + for i, s in enumerate(strs): + for j, t in enumerate(strs): + if i != j and check(s, t): break - if j == n: - ans = max(ans, len(strs[i])) + else: + ans = max(ans, len(s)) return ans diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.ts b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.ts new file mode 100644 index 0000000000000..fd48575eef427 --- /dev/null +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.ts @@ -0,0 +1,25 @@ +function findLUSlength(strs: string[]): number { + const n = strs.length; + let ans = -1; + const check = (s: string, t: string): boolean => { + const [m, n] = [s.length, t.length]; + let i = 0; + for (let j = 0; i < m && j < n; ++j) { + if (s[i] === t[j]) { + ++i; + } + } + return i === m; + }; + for (let i = 0; i < n; ++i) { + let x = strs[i].length; + for (let j = 0; j < n; ++j) { + if (i !== j && check(strs[i], strs[j])) { + x = -1; + break; + } + } + ans = Math.max(ans, x); + } + return ans; +}