Unit 4 of 9 · about 4 hours · derive, implement

Sequence and string dynamic programming

One 2D table shape carries LCS, edit distance and their reductions. The mismatch branch decides which problem you solved.

You'll be able to

  • Derive the state, recurrence, base cases and order for prefix-pair and single-index sequence problems.
  • Implement LCS, edit distance and quadratic LIS from blank files with differential tests.
  • Tell subsequence, substring and edit variants apart by their mismatch branch.

Covers

Sequence and string dynamic programming
1

Read

In this order, only the parts listed

0 of 4 done

  1. Two LIS recurrencesAlgorithms - dynamic programming

    Section 3.6 (pp. 109-110): First Recurrence: Is This Next? (, two indices) and Second Recurrence: What's Next? (, one index, filled in decreasing order)

  2. Edit distance recurrence and tableAlgorithms - dynamic programming

    Section 3.7 (pp. 111-114): three-way recurrence, row-major order, and the ALGORITHM vs ALTRUISTIC table with distance 6

  3. Quadratic LIS with recoveryDSA Handbook - dynamic programming

    The LIS: per-index as over smaller predecessors, with parent-pointer recovery and the counting variants

  4. Prefix decisions with validity gatesDSA Handbook - dynamic programming

    The recurrence, written once: for the prefix s[:i], a count of decodings for LC 91 Decode Ways and an OR over valid splits for LC 139 Word Break; Why , in one trace

2

Focus on

  • runs on one index because starting here needs only later starts; needs two indices because skipping is relative to a threshold.
  • Edit distance has three predecessors because the last column has three shapes. LCS mismatch only skips one side, which is an up or left step.
  • Subsequence inherits on mismatch; substring resets to 0. On ABCBDAB and BDCAB the answers are 4 and 2.
  • Decode Ways and Word Break share one skeleton: combines the valid splits that end at , a sum for Decode Ways and an OR for Word Break, each gated by a validity predicate.
Common mistakes · 3
  • Running LCS code on a longest-common-substring ask. Same table shape, different mismatch branch; the corner cell is not the substring answer.
  • Forgetting in . Then needs a special base case instead of falling out of the recurrence.
  • Counting 06 as one decoding in Decode Ways. A leading zero is invalid; gate 0 and out-of-range pairs to zero.
3

Try it yourself

Closed-book, before checking anything

Closed-book in Python 3.13: , , and . Then differentially test all three against brute force with Hypothesis on strings over and lists of length at most 8.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.State the recurrence and why decreasing order works.Answer

    of over with , with . Entry reads only entries , so filling from down keeps every read on a computed cell.

  2. 2.Why does edit distance need the diagonal predecessor while LCS mismatch does not?Answer

    Substitution consumes one character from each string in a single column, which is exactly the diagonal step with cost . LCS mismatch only skips a character from one side, which is an up or left step.

  3. 3.Word Break on catsandog with [cats, dog, sand, and, cat]: what does say and why?Answer

    False. No valid split covers the string: the OR over splits finds no chain of dictionary words reaching position 9.

  4. 4.When do you prefer the two-index over ?Answer

    Rarely for code; is shorter and space. earns its keep only when the threshold value matters per state, and both are time.

5

Practice

  • LeetCode 1143 - Longest Common Subsequence

    Attempt cold. Check , , , then submit.

    Attempt cold: write state, transition, base case and order first. After submitting, read the AlgoMaster Longest Common Subsequence lesson (https://algomaster.io/learn/dsa/longest-common-subsequence).

  • LeetCode 300 - Longest Increasing Subsequence

    Check and , then submit.

    After submitting, compare with the DSA Handbook lis-quadratic page, including the strict-increase gate.

You're done when

The reproduce numbers come out of a blank file, the brute-force differential tests pass, and you can call the mismatch branch for subsequence versus substring without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 6 more in the section
  • DSA Handbook - dynamic programmingHandbook Academy · primary

    Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.

  • Algorithms - dynamic programmingJeff Erickson · required

    Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.

Also in this section

  • AlgoMaster DSA - dynamic programming sliceAlgoMaster · required

    Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.

  • Hypothesis - exhaustive and differential DP testsHypothesis · support

    Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.

  • NeetCode - unseen mixed problemsNeetCode · optional

    Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.

  • Python sys: recursion limitPython Software Foundation · reference

    Read sys.getrecursionlimit and sys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default with getrecursionlimit (1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs.

  • Erickson lecture note D - Advanced Dynamic ProgrammingJeff Erickson · reference

    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in space.

  • Erickson lecture note J - Approximation AlgorithmsJeff Erickson · reference

    Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the GreedyLoadBalance heuristic, and Theorem J.1 with its factor-2 proof.