1class Solution:
2 def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3 """
4 text1: horizontally
5 text2: vertically
6 """
7 dp = [[0 for _ in range(len(text1)+1)] for _ in range(len(text2)+1)]
8
9 for row in range(1, len(text2)+1):
10 for col in range(1, len(text1)+1):
11 if text2[row-1]==text1[col-1]:
12 dp[row][col] = 1+ dp[row-1][col-1]
13 else:
14 dp[row][col] = max(dp[row-1][col], dp[row][col-1])
15 return dp[len(text2)][len(text1)]
1Input : arr[] = {3, 10, 2, 1, 20}
2Output : Length of LIS = 3
3The longest increasing subsequence is 3, 10, 20
4
5Input : arr[] = {3, 2}
6Output : Length of LIS = 1
7The longest increasing subsequences are {3} and {2}
8
9Input : arr[] = {50, 3, 10, 7, 40, 80}
10Output : Length of LIS = 4
11The longest increasing subsequence is {3, 7, 40, 80}
12