Skip to content

Commit f8dd2d9

Browse files
authored
Update 1143-longest-common-subsequence.js
1 parent bf47088 commit f8dd2d9

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

1143-longest-common-subsequence.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ const longestCommonSubsequence = function(text1, text2) {
2727
const len1 = text1.length
2828
const len2 = text2.length
2929
if(len1 === 0 || len2 === 0) return 0
30-
if(len1 < len2) return longestCommonSubsequence(text2, text1)
31-
const dp = Array(len1 + 1).fill(0)
32-
for(let i = 1; i <= len2; i++) {
30+
const dp = Array(len2 + 1).fill(0)
31+
for(let i = 1; i <= len1; i++) {
3332
let prev = 0
34-
for(let j = 1; j <= len1; j++) {
33+
for(let j = 1; j <= len2; j++) {
3534
const tmp = dp[j]
36-
if(text1[j - 1] === text2[i - 1]) dp[j] = Math.max(dp[j], prev + 1)
35+
if(text1[i - 1] === text2[j - 1]) dp[j] = Math.max(dp[j], prev + 1)
3736
else {
3837
dp[j] = Math.max(dp[j - 1], dp[j])
3938
}
4039
prev = tmp
4140
}
4241
}
43-
return dp[len1]
42+
return dp[len2]
4443
};

0 commit comments

Comments
 (0)