添加(0392.判断子序列.md):增加typescript版本

This commit is contained in:
Steve2020
2022-05-20 00:35:32 +08:00
parent aafc18ee12
commit 8759349af0

View File

@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
};
```
TypeScript
```typescript
function isSubsequence(s: string, t: string): boolean {
/**
dp[i][j]: s的前i-1个t的前j-1个最长公共子序列的长度
*/
const sLen: number = s.length,
tLen: number = t.length;
const dp: number[][] = new Array(sLen + 1).fill(0)
.map(_ => new Array(tLen + 1).fill(0));
for (let i = 1; i <= sLen; i++) {
for (let j = 1; j <= tLen; j++) {
if (s[i - 1] === t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[sLen][tLen] === s.length;
};
```
Go
```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)