In this tutorial, let us discuss about how the Longest Common Subsequence (LCS) is found in Python.

Problem Statement:

Given two strings text1 and text2, return the length of their lcs.

Subscribe to our Newsletter!

Never miss Upcoming articles, free e-books, weekly updates on latest news from Android and Coding Community.

subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, “ace” is a subsequence of “abcde” while “aec” is not). A common subsequence of two strings is a subsequence that is common to both strings.

If there is no common subsequence, return 0.

Related Links

Problem Description:

If t1 & t2 are given 2 sequences , then common subsequences of both t1 and t2 are x,y,z. Among those if length of string x is longer than other two strings, then x is the lcs.


Let us consider the sequences like t1={S,T,U,V} and t2={S,U,T,S,V}. Here Common Subsequences of length 2 are :{S,T},{S,U},{S,V},{T,V},{U,V} and Common Subsequences of length 3 are :{S,T,U},{S,U,V}. So, {S,T,U} & {S,U,V} are the lcs.

For Program/Source code & more explanation , please visit below YouTube video:

5 1 vote
Article Rating

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments