Build the text of a line diff like the ones in code review. Write renderDiff(oldLines, newLines) that returns a list of output lines: " " + line for a kept line, "- " + line for a deleted line, and "+ " + line for an inserted line.
Many diffs are equally short, so follow this exact rule. Let lcs[i][j] be the length of the longest common subsequence of oldLines from index i and newLines from index j. Start with i = 0 and j = 0 and repeat until both lists are used up:
i and j.newLines is used up, or oldLines isn't and lcs[i + 1][j] >= lcs[i][j + 1], delete oldLines[i] and move i.newLines[j] and move j.Input: oldLines = ["title","intro","body"], newLines = ["title","body","footer"] Output: [" title","- intro"," body","+ footer"]
intro is deleted and footer added; the other lines are kept.
Input: oldLines = ["a"], newLines = ["b"] Output: ["- a","+ b"]
On a tie, deleting comes first, so a changed line shows - then +.
0 <= oldLines.length, newLines.length <= 1000Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #714 Render a Line Diff. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.