Diff Two Texts Line by Line
Write the code, see it run, pass the checks.
The problem
- hard
- Code
Write diffLines(before, after), which compares two versions of a text, each given as an array of lines, the way a code review shows changes. Return an array of entries { type, line } in order, where type is "same" for a line kept from before, "removed" for a line only in before, and "added" for a line only in after. Reading the same and removed entries gives back before, and reading the same and added entries gives back after. Keep as many lines the same as possible: the same lines form a longest common subsequence of the two arrays. When several diffs keep that many, any of them will do, but within each run of changes between two same lines, list all the removed lines before the added ones. Two files of 1,000 lines must take well under a second.
Your code must pass
- Marks unchanged lines as same
- Shows added and removed lines
- Lists removed lines before added ones
- Keeps as many lines the same as possible
- Handles files of 1,000 lines