Sentence Similarity III - Problem

Imagine you're building a smart text editor that can detect if two sentences are essentially the same, even when one has some extra words inserted in the middle.

You're given two strings sentence1 and sentence2, each representing a sentence composed of words separated by single spaces (no leading or trailing spaces). Your task is to determine if these sentences are similar.

Two sentences are considered similar if you can insert an arbitrary sentence (possibly empty) into one of them to make both sentences identical. The inserted sentence must be properly separated by spaces.

Examples:

  • "Hello Jane" and "Hello my name is Jane" → Similar (insert "my name is" between "Hello" and "Jane")
  • "Frog cool" and "Frogs are cool" → Not similar ("Frog" ≠ "Frogs", can't insert partial words)

Return true if the sentences are similar, false otherwise.

Input & Output

example_1.py — Basic Insertion
$ Input: sentence1 = "Hello Jane", sentence2 = "Hello my name is Jane"
Output: true
💡 Note: We can insert "my name is" between "Hello" and "Jane" in sentence1 to make it identical to sentence2. The common prefix is "Hello" and common suffix is "Jane".
example_2.py — Invalid Word Modification
$ Input: sentence1 = "Frog cool", sentence2 = "Frogs are cool"
Output: false
💡 Note: "Frog" and "Frogs" are different words. We cannot insert a sentence to make "Frog" become "Frogs" since we can only insert complete words, not modify existing ones.
example_3.py — Empty Insertion
$ Input: sentence1 = "A", sentence2 = "a A b A"
Output: false
💡 Note: Case matters: "A" ≠ "a". Even though "A" appears in sentence2, the first "a" is lowercase, so no valid insertion exists.

Constraints

  • 1 ≤ sentence1.length, sentence2.length ≤ 100
  • sentence1 and sentence2 consist of lowercase and uppercase English letters and spaces
  • The words in sentence1 and sentence2 are separated by a single space
  • There are no leading or trailing spaces

Visualization

Tap to expand
The Sandwich Analogy: Sentence SimilaritySentence 1: "Hello Jane"HelloPrefix BreadJaneSuffix BreadEmpty SpaceSentence 2: "Hello my name is Jane"HellomynameisJaneSandwich FillingAlgorithm Visualization:1Find common prefix: "Hello" ✓2Find common suffix: "Jane" ✓3Check: prefix(1) + suffix(1) = 2 ≥ min(2,5) = 2 ✓✓ Sentences are similar!We can insert "my name is" between the bread slices "Hello" and "Jane"
Understanding the Visualization
1
Identify the bread slices
Find matching words at the start (prefix) and end (suffix)
2
Check the filling
See if the remaining middle part can be cleanly inserted
3
Validate the sandwich
Ensure the bread pieces cover the entire shorter sentence
Key Takeaway
🎯 Key Insight: Two sentences are similar if one can be formed by inserting words into a contiguous gap in the other, preserving common prefix and suffix words.
Asked in
Meta 15 Google 12 Microsoft 8 Apple 5
23.4K Views
Medium Frequency
~15 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