File tree 8 files changed +146
-1
lines changed
problemset/longest-uncommon-subsequence-2
8 files changed +146
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
<p align =" center " >
4
4
<!-- 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 " >
6
6
<!-- TOPICS COUNT END -->
7
7
<a href =" ./assets/docs/TOPICS.md " ><img src =" https://img.shields.io/badge/-题库目录-blue " alt =" 题库目录 " ></a >
8
8
<a href =" ./assets/docs/CATEGORIES.md " ><img src =" https://img.shields.io/badge/-题库分类-red " alt =" 题库分类 " ></a >
Original file line number Diff line number Diff line change 3332
3332
"path" : " ./problemset/largest-palindrome-product/README.md" ,
3333
3333
"difficulty" : " 困难"
3334
3334
},
3335
+ {
3336
+ "id" : " 522" ,
3337
+ "title" : " 最长特殊序列 II" ,
3338
+ "path" : " ./problemset/longest-uncommon-subsequence-2/README.md" ,
3339
+ "difficulty" : " 中等"
3340
+ },
3335
3341
{
3336
3342
"id" : " 699" ,
3337
3343
"title" : " 掉落的方块" ,
Original file line number Diff line number Diff line change 3029
3029
"url" : " https://leetcode-cn.com/problems/longest-uncommon-subsequence-i/" ,
3030
3030
"path" : " ./problemset/longest-uncommon-subsequence/README.md"
3031
3031
},
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
+ },
3032
3042
{
3033
3043
"id" : " 532" ,
3034
3044
"title" : {
Original file line number Diff line number Diff line change 634
634
| ---- | ---- |
635
635
| 204. [ 计数质数] ( ../../problemset/count-primes/README.md ) | 中等 |
636
636
| 479. [ 最大回文数乘积] ( ../../problemset/largest-palindrome-product/README.md ) | 困难 |
637
+ | 522. [ 最长特殊序列 II] ( ../../problemset/longest-uncommon-subsequence-2/README.md ) | 中等 |
637
638
| 699. [ 掉落的方块] ( ../../problemset/falling-squares/README.md ) | 困难 |
638
639
| 812. [ 最大三角形面积] ( ../../problemset/largest-triangle-area/README.md ) | 简单 |
639
640
| 1601. [ 最多可达成的换楼请求数目] ( ../../problemset/maximum-number-of-achievable-transfer-requests/README.md ) | 困难 |
Original file line number Diff line number Diff line change 606
606
607
607
[ 521. 最长特殊序列] ( ../../problemset/longest-uncommon-subsequence/README.md )
608
608
609
+ [ 522. 最长特殊序列 II] ( ../../problemset/longest-uncommon-subsequence-2/README.md )
610
+
609
611
[ 532. 数组中的 k-diff 数对] ( ../../problemset/k-diff-pairs-in-an-array/README.md )
610
612
611
613
[ 537. 复数乘法] ( ../../problemset/complex-number-multiplication/README.md )
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments