File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
201
201
};
202
202
```
203
203
204
+ TypeScript:
205
+
206
+ ``` typescript
207
+ function isSubsequence(s : string , t : string ): boolean {
208
+ /**
209
+ dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度
210
+ */
211
+ const sLen: number = s .length ,
212
+ tLen: number = t .length ;
213
+ const dp: number [][] = new Array (sLen + 1 ).fill (0 )
214
+ .map (_ => new Array (tLen + 1 ).fill (0 ));
215
+ for (let i = 1 ; i <= sLen ; i ++ ) {
216
+ for (let j = 1 ; j <= tLen ; j ++ ) {
217
+ if (s [i - 1 ] === t [j - 1 ]) {
218
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
219
+ } else {
220
+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
221
+ }
222
+ }
223
+ }
224
+ return dp [sLen ][tLen ] === s .length ;
225
+ };
226
+ ```
227
+
204
228
Go:
229
+
205
230
``` go
206
231
func isSubsequence (s string , t string ) bool {
207
232
dp := make ([][]int ,len (s)+1 )
You can’t perform that action at this time.
0 commit comments