64
64
65
65
### 方法一:判断子序列
66
66
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$ 的子序列 。
68
68
69
- 其中,$check(a,b)$ 用于判断字符串 $b$ 是否为字符串 $a$ 的子序列。
69
+ 判断字符串 $s$ 是否独有,只需要取字符串 $s$ 本身,与字符串列表的其他字符串比较即可。如果存在 $s$ 是其他字符串的子序列,则 $s$ 不是独有的。否则,字符串 $s$ 是独有的。我们取所有独有字符串中长度最长的字符串即可。
70
+
71
+ 时间复杂度 $O(n^2 \times m)$,其中 $n$ 是字符串列表的长度,而 $m$ 是字符串的平均长度。空间复杂度 $O(1)$。
70
72
71
73
<!-- tabs:start -->
72
74
@@ -75,26 +77,21 @@ tags:
75
77
``` python
76
78
class Solution :
77
79
def findLUSlength (self , strs : List[str ]) -> int :
78
- def check (a , b ):
80
+ def check (s : str , t : str ):
79
81
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 )
85
87
86
- n = len (strs)
87
88
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):
95
92
break
96
- if j == n :
97
- ans = max (ans, len (strs[i] ))
93
+ else :
94
+ ans = max (ans, len (s ))
98
95
return ans
99
96
```
100
97
@@ -104,30 +101,29 @@ class Solution:
104
101
class Solution {
105
102
public int findLUSlength (String [] strs ) {
106
103
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();
108
107
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 ;
113
110
break ;
114
111
}
115
112
}
116
- if (j == n) {
117
- ans = Math . max(ans, strs[i]. length());
118
- }
113
+ ans = Math . max(ans, x);
119
114
}
120
115
return ans;
121
116
}
122
117
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;
128
124
}
129
125
}
130
- return j == b . length() ;
126
+ return i == m ;
131
127
}
132
128
}
133
129
```
@@ -139,57 +135,91 @@ class Solution {
139
135
public:
140
136
int findLUSlength(vector<string >& strs) {
141
137
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();
143
151
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
+ }
146
156
}
147
- if (j == n) ans = max(ans, (int) strs [ i ] .size() );
157
+ ans = max(ans, x );
148
158
}
149
159
return ans;
150
160
}
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
- }
158
161
};
159
162
```
160
163
161
164
#### Go
162
165
163
166
```go
164
167
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++
170
175
}
171
176
}
172
- return j == len (b)
177
+ return i == m
173
178
}
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
182
184
break
183
185
}
184
186
}
185
- if j == n && ans < len (strs[i]) {
186
- ans = len (strs[i])
187
- }
187
+ ans = max(ans, x)
188
188
}
189
189
return ans
190
190
}
191
191
```
192
192
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
+
193
223
<!-- tabs: end -->
194
224
195
225
<!-- solution: end -->
0 commit comments