Diff Tool Builder - Problem
Build a text diff tool that compares two strings line by line, showing additions (+), deletions (-), and unchanged lines.
Requirements:
- Split both strings into lines
- Compare each line and mark as: unchanged, added (+), or deleted (-)
- Return an array of strings where each string represents one line with its prefix
- Unchanged lines have no prefix, additions start with '+', deletions start with '-'
Note: This is a simplified diff tool - you don't need to implement complex alignment algorithms.
Input & Output
Example 1 — Basic Diff
$
Input:
text1 = "line1\nline2\nline3", text2 = "line1\nline4\nline3"
›
Output:
["line1", "-line2", "line3", "+line4"]
💡 Note:
Line1 and line3 are unchanged, line2 was deleted from text1, line4 was added in text2
Example 2 — All Changes
$
Input:
text1 = "hello\nworld", text2 = "goodbye\nuniverse"
›
Output:
["-hello", "-world", "+goodbye", "+universe"]
💡 Note:
No common lines - all lines from text1 are deleted, all from text2 are added
Example 3 — Empty Text
$
Input:
text1 = "", text2 = "new line"
›
Output:
["+new line"]
💡 Note:
Empty first text, so the line in text2 is an addition
Constraints
- 0 ≤ text1.length, text2.length ≤ 104
- Each line contains only printable ASCII characters
- Lines are separated by '\n' character
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code