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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code