Problem 584296 · medium · Phase 05 Advanced Algorithms & Graphs

Longest Shared Run

dynamic programming · string DP · substring

Two lab samples are recorded as lowercase strings a and b. A shared run is a block of consecutive characters that appears (contiguously) in both. Return the length of the longest shared run; return 0 if the strings share no character.

Examples

Input:  a = "harborlight", b = "neighborly"
Output: 4
Explanation: "borl" appears in both.

Input:  a = "abc", b = "xyz"
Output: 0

Constraints

  • 0 <= len(a), len(b) <= 300
  • Lowercase letters only.
  • Target complexity: O(len(a) * len(b)).

Goals

  • Distinguish a contiguous substring state from the subsequence state
  • Reset the table entry to zero when characters differ
Starting Python…