Skip to content

Commit c43355d

Browse files
authored
add js solution for longestCommonSubsequence
1 parent 81f74d8 commit c43355d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

problems/1143.最长公共子序列.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,24 @@ func max(a,b int)int {
197197
}
198198
```
199199

200+
Javascript:
201+
```javascript
202+
const longestCommonSubsequence = (text1, text2) => {
203+
let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0));
204+
205+
for(let i = 1; i <= text1.length; i++) {
206+
for(let j = 1; j <= text2.length; j++) {
207+
if(text1[i-1] === text2[j-1]) {
208+
dp[i][j] = dp[i-1][j-1] +1;;
209+
} else {
210+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1])
211+
}
212+
}
213+
}
200214

215+
return dp[text1.length][text2.length];
216+
};
217+
```
201218

202219

203220
-----------------------

0 commit comments

Comments
 (0)