Skip to content

Commit aff8e1b

Browse files
committed
feat: leetcode 522
1 parent dce7ea1 commit aff8e1b

File tree

8 files changed

+146
-1
lines changed

8 files changed

+146
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:414-green" alt="进度:414">
5+
<img src="https://img.shields.io/badge/-进度:415-green" alt="进度:415">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -3332,6 +3332,12 @@
33323332
"path": "./problemset/largest-palindrome-product/README.md",
33333333
"difficulty": "困难"
33343334
},
3335+
{
3336+
"id": "522",
3337+
"title": "最长特殊序列 II",
3338+
"path": "./problemset/longest-uncommon-subsequence-2/README.md",
3339+
"difficulty": "中等"
3340+
},
33353341
{
33363342
"id": "699",
33373343
"title": "掉落的方块",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,16 @@
30293029
"url": "https://leetcode-cn.com/problems/longest-uncommon-subsequence-i/",
30303030
"path": "./problemset/longest-uncommon-subsequence/README.md"
30313031
},
3032+
{
3033+
"id": "522",
3034+
"title": {
3035+
"cn": "最长特殊序列 II",
3036+
"en": "longest-uncommon-subsequence-2"
3037+
},
3038+
"difficulty": "中等",
3039+
"url": "https://leetcode.cn/problems/longest-uncommon-subsequence-ii/",
3040+
"path": "./problemset/longest-uncommon-subsequence-2/README.md"
3041+
},
30323042
{
30333043
"id": "532",
30343044
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@
634634
| ---- | ---- |
635635
| 204. [计数质数](../../problemset/count-primes/README.md) | 中等 |
636636
| 479. [最大回文数乘积](../../problemset/largest-palindrome-product/README.md) | 困难 |
637+
| 522. [最长特殊序列 II](../../problemset/longest-uncommon-subsequence-2/README.md) | 中等 |
637638
| 699. [掉落的方块](../../problemset/falling-squares/README.md) | 困难 |
638639
| 812. [最大三角形面积](../../problemset/largest-triangle-area/README.md) | 简单 |
639640
| 1601. [最多可达成的换楼请求数目](../../problemset/maximum-number-of-achievable-transfer-requests/README.md) | 困难 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@
606606

607607
[521. 最长特殊序列](../../problemset/longest-uncommon-subsequence/README.md)
608608

609+
[522. 最长特殊序列 II](../../problemset/longest-uncommon-subsequence-2/README.md)
610+
609611
[532. 数组中的 k-diff 数对](../../problemset/k-diff-pairs-in-an-array/README.md)
610612

611613
[537. 复数乘法](../../problemset/complex-number-multiplication/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 最长特殊序列 II
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode.cn/problems/longest-uncommon-subsequence-ii/
6+
7+
## 题目
8+
9+
给定字符串列表 `strs` ,返回其中 最长的特殊序列 。如果最长特殊序列不存在,返回` -1`
10+
11+
**特殊序列** 定义如下:该序列为某字符串** 独有的子序列(即不能是其他字符串的子序列)**
12+
13+
 `s` 的 子序列可以通过删去字符串 `s` 中的某些字符实现。
14+
15+
- 例如,`"abc"` 是 `"aebdc"` 的子序列,因为您可以删除`"aebdc"`中的下划线字符来得到 `"abc" ``"aebdc"`的子序列还包括`"aebdc"``"aeb"` 和 `""` (空字符串)。
16+
 
17+
### 示例
18+
19+
#### 示例 1:
20+
21+
```
22+
输入: strs = ["aba","cdc","eae"]
23+
输出: 3
24+
```
25+
26+
#### 示例 2:
27+
```
28+
输入: strs = ["aaa","aaa","aa"]
29+
输出: -1
30+
```
31+
32+
## 解题
33+
34+
```ts
35+
/**
36+
* 枚举每个字符串
37+
* @desc 时间复杂度 O(N²) 空间复杂度 O(1)
38+
* @param strs
39+
* @returns
40+
*/
41+
export function findLUSlength(strs: string[]): number {
42+
const len = strs.length
43+
let ans = -1
44+
for (let i = 0; i < len; i++) {
45+
let check = true
46+
for (let j = 0; j < len; j++) {
47+
if (i !== j && isSubseq(strs[i], strs[j])) {
48+
check = false
49+
break
50+
}
51+
}
52+
53+
if (check)
54+
ans = Math.max(ans, strs[i].length)
55+
}
56+
57+
return ans
58+
59+
function isSubseq(s: string, t: string) {
60+
let ptS = 0
61+
let ptT = 0
62+
while (ptS < s.length && ptT < t.length) {
63+
if (s[ptS] === t[ptT]) ptS++
64+
65+
ptT++
66+
}
67+
return ptS === s.length
68+
}
69+
}
70+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { findLUSlength } from '.'
3+
4+
describe('最长特殊序列 II', () => {
5+
testCase(findLUSlength)
6+
})
7+
8+
function testCase(fn: (strs: string[]) => number) {
9+
it.each([
10+
[
11+
['aba', 'cdc', 'eae'],
12+
3,
13+
],
14+
[
15+
['aaa', 'aaa', 'aa'],
16+
-1,
17+
],
18+
])('示例%#', (strs, expected) => {
19+
expect(fn(strs)).toBe(expected)
20+
})
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 枚举每个字符串
3+
* @desc 时间复杂度 O(N²) 空间复杂度 O(1)
4+
* @param strs
5+
* @returns
6+
*/
7+
export function findLUSlength(strs: string[]): number {
8+
const len = strs.length
9+
let ans = -1
10+
for (let i = 0; i < len; i++) {
11+
let check = true
12+
for (let j = 0; j < len; j++) {
13+
if (i !== j && isSubseq(strs[i], strs[j])) {
14+
check = false
15+
break
16+
}
17+
}
18+
19+
if (check)
20+
ans = Math.max(ans, strs[i].length)
21+
}
22+
23+
return ans
24+
25+
function isSubseq(s: string, t: string) {
26+
let ptS = 0
27+
let ptT = 0
28+
while (ptS < s.length && ptT < t.length) {
29+
if (s[ptS] === t[ptT]) ptS++
30+
31+
ptT++
32+
}
33+
return ptS === s.length
34+
}
35+
}

0 commit comments

Comments
 (0)