Skip to the code
cf.
+25 XP

#715 Revision Similarity Score

medium

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.

Example 1

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.

Example 2

Input: oldText = "", newText = "  "
Output: 1

Neither text has words, so they count as identical.

Constraints

  • Each text has at most 1000 words.
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).