Skip to content

Commit 41fd9c0

Browse files
authored
longest common subsequence solution
1 parent d0ffa80 commit 41fd9c0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
int longestCommonSubsequence(String text1, String text2) {
3+
final dp = List.generate(
4+
text1.length + 1,
5+
(_) => List.filled(text2.length + 1, 0),
6+
);
7+
for (int i = 1; i <= text1.length; i++) {
8+
for (int j = 1; j <= text2.length; j++) {
9+
dp[i][j] = text1[i - 1] == text2[j - 1] ? dp[i - 1][j - 1] + 1 : max(dp[i - 1][j], dp[i][j - 1]);
10+
}
11+
}
12+
return dp[text1.length][text2.length];
13+
}
14+
}

0 commit comments

Comments
 (0)