Skip to the code
cf.
+10 XP

#530 Unchanged Lines Between Two Versions

easy

A code review tool compares two versions of a file, given as arrays of lines: oldLines and newLines. Lines that appear in both versions in the same relative order are shown as unchanged; everything else is shown as removed or added.

Write unchangedLines(oldLines, newLines) that returns the largest possible number of unchanged lines: the length of the longest common subsequence of the two arrays. Lines match only if they are exactly equal strings.

Example 1

Input: oldLines = ["<div>","<h1>","<p>","</div>"], newLines = ["<div>","<p>","<span>","</div>"]
Output: 3

<div>, <p>, and </div> appear in both, in the same order.

Example 2

Input: oldLines = ["x = 1"], newLines = ["y = 2"]
Output: 0

The only lines differ.

Constraints

  • 0 <= oldLines.length, newLines.length <= 1000
  • Each line has at most 100 characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).