We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 723d246 commit b8676d5Copy full SHA for b8676d5
Algorithms/Medium/1143_LongestCommonSubsequence/Solution.py
@@ -0,0 +1,20 @@
1
+class Solution:
2
+ def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3
+ text1_length = len(text1)
4
+ text2_length = len(text2)
5
+
6
+ lcs_array = self._get_initialized_lcs_array(text1_length, text2_length)
7
8
+ for idx_2, text2_char in enumerate(text2):
9
+ row = idx_2 + 1
10
+ for idx_1, text1_char in enumerate(text1):
11
+ col = idx_1 + 1
12
+ if text1_char == text2_char:
13
+ lcs_array[row][col] = lcs_array[row-1][col-1] + 1
14
+ else:
15
+ lcs_array[row][col] = max(lcs_array[row][col-1], lcs_array[row-1][col])
16
17
+ return lcs_array[row][col]
18
19
+ def _get_initialized_lcs_array(self, text1_length, text2_length):
20
+ return [[0]*(text1_length+1) for _ in range(text2_length + 1)]
0 commit comments