Skip to content

Commit b8676d5

Browse files
authored
Add solution for problem 1143. (#84)
1 parent 723d246 commit b8676d5

File tree

1 file changed

+20
-0
lines changed
  • Algorithms/Medium/1143_LongestCommonSubsequence

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)