Dynamic programming · Problem 3 of 3

Longest shared sequence between two edit logs

Pattern: Longest Common Subsequence: 2D DP

mediumNot solved yet

The problem

Two people's edit logs are recorded as strings of action codes, one letter per action. Write a function that returns the length of the longest sequence of actions that appears in both logs in the same order. The actions don't have to be next to each other: you can skip actions in either log.

text
1longest_shared("ABCBDAB", "BDCABA")  ->  4
2longest_shared("AXY", "AYX")         ->  2
3longest_shared("ABC", "DEF")         ->  0

In the first example, "BCBA" appears in both in order (so do "BDAB" and "BCAB").

Write longest_shared(a, b) in Python or longestShared(a, b) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 22 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.