Problem
You are given two strings sentence1
and sentence2
, each representing a sentence composed of words. A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of only uppercase and lowercase English characters.
Two sentences s1
and s2
are considered similar if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. Note that the inserted sentence must be separated from existing words by spaces.
For example,
s1 = "Hello Jane"
ands2 = "Hello my name is Jane"
can be made equal by inserting"my name is"
between"Hello"
and"Jane"
in s1.s1 = "Frog cool"
ands2 = "Frogs are cool"
are not similar, since although there is a sentence"s are"
inserted intos1
, it is not separated from"Frog"
by a space.
Given two sentences sentence1
and sentence2
, return true if sentence1
and sentence2
are similar. Otherwise, return false.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - Scan shorter sentence
By identifying the longest prefix and suffix that are common between the two word lists, we can determine the parts of the sentences that could potentially be identical if additional words (a valid phrase) are inserted between them.
The validity of insertion comes down to whether the non-matching middle portions (after excluding the prefix and suffix) can logically align with an inserted sentence or not.
Here is the approach:
- Splitting Sentences into Words:
- First, we split both sentences into arrays of words using space as the delimiter.
- For example:
sentence1 = "My name is Haley"
results inwords1 = ["My", "name", "is", "Haley"]
sentence2 = "My Haley"
results inwords2 = ["My", "Haley"]
- Finding Common Prefix:
- We traverse from the beginning of both word arrays, counting how many words match at corresponding positions (common prefix).
- If words1[i] == words2[i], we increment
i
.
- Finding Common Suffix:
- Similarly, we traverse from the end of both word arrays, counting how many words match at corresponding positions (common suffix).
- If words1[n1 - 1 - j] == words2[n2 - 1 - j], we increment
j
.
- Checking the Condition:
- If the sum of the lengths of the common prefix and common suffix is at least the length of the shorter sentence, then the sentences can be made similar by inserting the missing words in the middle.
- Mathematically, the condition is:
i + j >= min(n1, n2)
, wheren1
andn2
are the lengths ofwords1
andwords2
, respectively.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(m + n)
,m
is the length ofsentence1
andn
is the length ofsentence2
. In the worst case, we need to compare all words in both sentences, which gives us a linear time complexity relative to the total number of words in both sentences. - 🧺 Space complexity:
O(m + n)
, The space complexity is also linear in terms of the number of words split in both sentences. This mainly comes from storing the split words in arrays or lists.