A document editor's version history labels each revision as a small edit or a rewrite, based on how similar it is to the previous one.
Split each text into words on whitespace (ignore empty pieces). With n and m words and k the length of the longest common subsequence of the two word lists, the similarity is 2 * k / (n + m). Two texts with no words at all have similarity 1.
Write similarity(oldText, newText) that returns this number. Answers within 1e-5 are accepted.
Input: oldText = "the quick brown fox", newText = "the brown fox jumps" Output: 0.75
the brown fox is shared: 2 * 3 / (4 + 4) = 0.75.
Input: oldText = "", newText = " " Output: 1
Neither text has words, so they count as identical.
1000 words.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).
I'm here to help with #715 Revision Similarity Score. 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.