File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Algorithms/Medium/1143_LongestCommonSubsequence Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change
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 )]
You can’t perform that action at this time.
0 commit comments