Skip to content

Commit 51147bf

Browse files
authored
feat: add solutions to lc problem: No.522 (#3114)
No.0522.Longest Uncommon Subsequence II
1 parent 1206f97 commit 51147bf

File tree

7 files changed

+264
-177
lines changed

7 files changed

+264
-177
lines changed

solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md

Lines changed: 90 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ tags:
6464

6565
### 方法一:判断子序列
6666

67-
判断是否独有,只需要取字符串 $s$ 本身,与其他字符串比较即可。题目可以转化为:获取**非其他字符串子序列**的字符串的最大长度。若不存在,返回 -1
67+
我们定义一个函数 $check(s, t)$,用于判断字符串 $s$ 是否是字符串 $t$ 的子序列。我们可以使用双指针的方法,初始化两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 和字符串 $t$ 的开头,然后不断移动指针 $j$,如果 $s[i]$ 和 $t[j]$ 相等,则移动指针 $i$,最后判断 $i$ 是否等于 $s$ 的长度即可。若 $i$ 等于 $s$ 的长度,则说明 $s$ 是 $t$ 的子序列
6868

69-
其中,$check(a,b)$ 用于判断字符串 $b$ 是否为字符串 $a$ 的子序列。
69+
判断字符串 $s$ 是否独有,只需要取字符串 $s$ 本身,与字符串列表的其他字符串比较即可。如果存在 $s$ 是其他字符串的子序列,则 $s$ 不是独有的。否则,字符串 $s$ 是独有的。我们取所有独有字符串中长度最长的字符串即可。
70+
71+
时间复杂度 $O(n^2 \times m)$,其中 $n$ 是字符串列表的长度,而 $m$ 是字符串的平均长度。空间复杂度 $O(1)$。
7072

7173
<!-- tabs:start -->
7274

@@ -75,26 +77,21 @@ tags:
7577
```python
7678
class Solution:
7779
def findLUSlength(self, strs: List[str]) -> int:
78-
def check(a, b):
80+
def check(s: str, t: str):
7981
i = j = 0
80-
while i < len(a) and j < len(b):
81-
if a[i] == b[j]:
82-
j += 1
83-
i += 1
84-
return j == len(b)
82+
while i < len(s) and j < len(t):
83+
if s[i] == t[j]:
84+
i += 1
85+
j += 1
86+
return i == len(s)
8587

86-
n = len(strs)
8788
ans = -1
88-
89-
for i in range(n):
90-
j = 0
91-
while j < n:
92-
if i == j or not check(strs[j], strs[i]):
93-
j += 1
94-
else:
89+
for i, s in enumerate(strs):
90+
for j, t in enumerate(strs):
91+
if i != j and check(s, t):
9592
break
96-
if j == n:
97-
ans = max(ans, len(strs[i]))
93+
else:
94+
ans = max(ans, len(s))
9895
return ans
9996
```
10097

@@ -104,30 +101,29 @@ class Solution:
104101
class Solution {
105102
public int findLUSlength(String[] strs) {
106103
int ans = -1;
107-
for (int i = 0, j = 0, n = strs.length; i < n; ++i) {
104+
int n = strs.length;
105+
for (int i = 0, j; i < n; ++i) {
106+
int x = strs[i].length();
108107
for (j = 0; j < n; ++j) {
109-
if (i == j) {
110-
continue;
111-
}
112-
if (check(strs[j], strs[i])) {
108+
if (i != j && check(strs[i], strs[j])) {
109+
x = -1;
113110
break;
114111
}
115112
}
116-
if (j == n) {
117-
ans = Math.max(ans, strs[i].length());
118-
}
113+
ans = Math.max(ans, x);
119114
}
120115
return ans;
121116
}
122117

123-
private boolean check(String a, String b) {
124-
int j = 0;
125-
for (int i = 0; i < a.length() && j < b.length(); ++i) {
126-
if (a.charAt(i) == b.charAt(j)) {
127-
++j;
118+
private boolean check(String s, String t) {
119+
int m = s.length(), n = t.length();
120+
int i = 0;
121+
for (int j = 0; i < m && j < n; ++j) {
122+
if (s.charAt(i) == t.charAt(j)) {
123+
++i;
128124
}
129125
}
130-
return j == b.length();
126+
return i == m;
131127
}
132128
}
133129
```
@@ -139,57 +135,91 @@ class Solution {
139135
public:
140136
int findLUSlength(vector<string>& strs) {
141137
int ans = -1;
142-
for (int i = 0, j = 0, n = strs.size(); i < n; ++i) {
138+
int n = strs.size();
139+
auto check = [&](const string& s, const string& t) {
140+
int m = s.size(), n = t.size();
141+
int i = 0;
142+
for (int j = 0; i < m && j < n; ++j) {
143+
if (s[i] == t[j]) {
144+
++i;
145+
}
146+
}
147+
return i == m;
148+
};
149+
for (int i = 0, j; i < n; ++i) {
150+
int x = strs[i].size();
143151
for (j = 0; j < n; ++j) {
144-
if (i == j) continue;
145-
if (check(strs[j], strs[i])) break;
152+
if (i != j && check(strs[i], strs[j])) {
153+
x = -1;
154+
break;
155+
}
146156
}
147-
if (j == n) ans = max(ans, (int) strs[i].size());
157+
ans = max(ans, x);
148158
}
149159
return ans;
150160
}
151-
152-
bool check(string a, string b) {
153-
int j = 0;
154-
for (int i = 0; i < a.size() && j < b.size(); ++i)
155-
if (a[i] == b[j]) ++j;
156-
return j == b.size();
157-
}
158161
};
159162
```
160163
161164
#### Go
162165
163166
```go
164167
func findLUSlength(strs []string) int {
165-
check := func(a, b string) bool {
166-
j := 0
167-
for i := 0; i < len(a) && j < len(b); i++ {
168-
if a[i] == b[j] {
169-
j++
168+
ans := -1
169+
check := func(s, t string) bool {
170+
m, n := len(s), len(t)
171+
i := 0
172+
for j := 0; i < m && j < n; j++ {
173+
if s[i] == t[j] {
174+
i++
170175
}
171176
}
172-
return j == len(b)
177+
return i == m
173178
}
174-
175-
ans := -1
176-
for i, j, n := 0, 0, len(strs); i < n; i++ {
177-
for j = 0; j < n; j++ {
178-
if i == j {
179-
continue
180-
}
181-
if check(strs[j], strs[i]) {
179+
for i, s := range strs {
180+
x := len(s)
181+
for j, t := range strs {
182+
if i != j && check(s, t) {
183+
x = -1
182184
break
183185
}
184186
}
185-
if j == n && ans < len(strs[i]) {
186-
ans = len(strs[i])
187-
}
187+
ans = max(ans, x)
188188
}
189189
return ans
190190
}
191191
```
192192

193+
#### TypeScript
194+
195+
```ts
196+
function findLUSlength(strs: string[]): number {
197+
const n = strs.length;
198+
let ans = -1;
199+
const check = (s: string, t: string): boolean => {
200+
const [m, n] = [s.length, t.length];
201+
let i = 0;
202+
for (let j = 0; i < m && j < n; ++j) {
203+
if (s[i] === t[j]) {
204+
++i;
205+
}
206+
}
207+
return i === m;
208+
};
209+
for (let i = 0; i < n; ++i) {
210+
let x = strs[i].length;
211+
for (let j = 0; j < n; ++j) {
212+
if (i !== j && check(strs[i], strs[j])) {
213+
x = -1;
214+
break;
215+
}
216+
}
217+
ans = Math.max(ans, x);
218+
}
219+
return ans;
220+
}
221+
```
222+
193223
<!-- tabs:end -->
194224

195225
<!-- solution:end -->

0 commit comments

Comments
 (0)