• + 0 comments

    My solution based on https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/:

    def _lcs(s1, s2):
        memo=[[0]*(len(s1)+1) for _ in range((len(s2)+1))]
        for i in range(1, len(s1)+1):
            for j in range(1, len(s2)+1):            
                if s1[i-1]==s2[j-1] :          
                    memo[i][j]=memo[i-1][j-1]+1
                else:
                    memo[i][j]=max(memo[i-1][j], memo[i][j-1])
        return memo[-1][-1]
    
    
    def commonChild(s1, s2):
        return _lcs(s1, s2)
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        s1 = input()
    
        s2 = input()
    
        result = commonChild(s1, s2)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()