class Solution:
def LCS(self , str1 , str2 ):
if len(str1) == 1:
return str1
if len(str2) == 1:
return str2
dp = [[0 for i in range(len(str1)+1)] for j in range(len(str2)+1)]
maxlen = 0
for i in range(len(str2)):
for j in range(len(str1)):
if str2[i] == str1[j]:
dp[i+1][j+1] = dp[i][j] + 1
if dp[i+1][j+1] > maxlen:
maxlen = dp[i+1][j+1]
pos = j+1 # position
res = ''
j = pos - maxlen
while j < pos:
res += str1[j]
j += 1
return res