Longest Common Subsequence

Tushar Roy - Coding Made Simple · 850K views · 2015 · 7:55

Open on YouTube

Algorithms & data structuresEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining or learning the LCS dynamic programming table and how to backtrack through it to recover the subsequence.

Recommended by 1 people across 1 different months, 2016 to 2020.

What it is

A short whiteboard tutorial by Tushar Roy that explains the longest common subsequence problem and its dynamic programming solution. It builds the length table step by step and then backtracks through it to recover the actual subsequence.

What people said

Recommends nAviD · 2016-02 · Stack Overflow · on “Algorithm to update only the changed innerHTML of the document”

You must implement the LCS [LINK] (Longest Common Subsequence). To understand better of this algorithm you can watch this [this video] youtube video. Also It's easier to first study Longest Common Substring [LINK] .

Cites loremIpsum1771 · 2016-08 · Stack Overflow · on “Finding the longest Common Subsequence from a table”

I have two input strings for the LCS problem: 1: ABCDGH 2:AEDFHR The following table represents the Dynamic Programming Solution to the bottom-up table for the length of the LCS: [LINK] Based on the method provided in this video [this video] , when trying to find the actual letters in the LCS, you start from the end of the table and go backwards. If the cells to the left and right aren't the same as the current one and the cell diagonal is one less, then you know the character in the current column is included and you move back diagonally. Otherwise you either move to the left or the right. Following that approach, you would have this sequence of movements (H,R), (H,H),and then to (F,G). But once

Cites mourinho · 2018-02 · Stack Overflow · on “Longest Common Subsequence in Python”

I am trying to find the longest common subsequence between two strings. I watched this tutoial [this video] and wrote: # Longest Common Subsequence def lcs(s1, s2): matrix = [ [0 for x in range(len(s2))] for x in range(len(s1)) ] cs = "" for i in range(len(s1)): for j in range(len(s2)): if s1[i]==s2[j]: if i==0 or j==0: matrix[i][j] = 1 cs += s1[i] else: matrix[i][j] = matrix[i-1][j-1] + 1 cs += s1[i] else: if i==0 or j==0: matrix[i][j] = 0 else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1]) return matrix[len(s1)-1][len(s2)-1], cs print(lcs("abcdaf", "acbcf")) I get (3, 'abccaf') This is clearly wrong it should be 4 abcf. Not sure what step is going wrong. One general question is how long do

Cites Shone Saji · 2020-05 · Stack Overflow · on “Can someone tell me if this approach to the Longest Common Subsequence of Three Sequences problem is correct”

Is it possible to do Longest Common Subsequence of Three Sequences using the same algorithm used for solving Longest Common Subsequence of Two Sequences, My idea is that i find the following common subsequences. Suppose that the three sequences are 'a','b' and 'c'. So i will pass to the function used to solve Longest Common Subsequence of Two Sequences as 1)(a,b) 2)(b,c) and 3)(c,a) and store the length from each of the three cases in an array and finally output the minimum among the three. I have been able to make it till the the last test case and I would appreciate if someone can suggest whether the above mentioned idea is correct or not I have used the table approach to solve Longest Com