Text Similarity Score - Problem

Calculate the similarity between two text documents by finding the longest common word sequence (not substring).

Given two documents represented as arrays of words, find the length of the longest sequence of words that appears in both documents in the same order, but not necessarily consecutively.

For example, if document A contains ["the", "quick", "brown", "fox"] and document B contains ["a", "quick", "brown", "cat"], the longest common word sequence is ["quick", "brown"] with length 2.

This is similar to the classic Longest Common Subsequence problem but applied to word sequences instead of character sequences.

Input & Output

Example 1 — Basic Common Words
$ Input: doc1 = ["the", "cat", "sat"], doc2 = ["a", "cat", "ran"]
Output: 1
💡 Note: The longest common word sequence is ["cat"] with length 1. The word "cat" appears in both documents in the same relative position.
Example 2 — Multiple Common Words
$ Input: doc1 = ["quick", "brown", "fox"], doc2 = ["the", "quick", "brown", "dog"]
Output: 2
💡 Note: The longest common word sequence is ["quick", "brown"] with length 2. Both words appear in the same order in both documents.
Example 3 — No Common Words
$ Input: doc1 = ["hello", "world"], doc2 = ["good", "bye"]
Output: 0
💡 Note: There are no common words between the two documents, so the similarity score is 0.

Constraints

  • 1 ≤ doc1.length, doc2.length ≤ 1000
  • 1 ≤ doc1[i].length, doc2[i].length ≤ 100
  • All words consist of lowercase English letters

Visualization

Tap to expand
INPUT DOCUMENTSALGORITHM STEPSFINAL RESULTthecatsatDocument 1acatranDocument 2Find longest commonword sequence1Create DP table(m+1) × (n+1) dimensions2Fill base casesInitialize empty document rows3Compare wordsMatch: dp[i][j] = dp[i-1][j-1] + 14Take maximumNo match: max(left, top)Time: O(m×n)Space: O(m×n)Similarity Score1LCS LengthCommon Words"cat"Documents share 1word in common orderKey Insight:Dynamic programming builds the solution incrementally, avoiding redundant comparisonsby reusing previously calculated subsequence lengths in a 2D table.TutorialsPoint - Text Similarity Score | 2D Dynamic Programming
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen