The Sentence Similarity III problem asks you to determine whether two sentences, represented as strings sentence1
and sentence2
, are similar under a specific definition.
Two sentences are considered similar if it is possible to insert an arbitrary (possibly empty) sequence of words into one of them (at any position, including the start or end) so that the two sentences become exactly equal.
Key constraints:
True
if the two sentences are similar according to the rule above, and False
otherwise.
At first glance, the problem seems to ask if we can make two sentences equal by inserting words anywhere. But a closer look reveals:
We'll solve the problem efficiently by following these steps:
Example:
sentence1 = "my name is alice"
sentence2 = "my alice"
Step-by-step:
words1 = ["my", "name", "is", "alice"]
words2 = ["my", "alice"]
words2
(length 2)words2
(2 words total)sentence1
can become sentence2
by inserting "name is" in the middle.True
.
sentence1 = "hello world"
sentence2 = "world hello"
Brute-force approach:
class Solution:
def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
words1 = sentence1.split()
words2 = sentence2.split()
# Always let words1 be the longer one
if len(words1) < len(words2):
words1, words2 = words2, words1
n, m = len(words1), len(words2)
# Check prefix
i = 0
while i < m and words1[i] == words2[i]:
i += 1
# Check suffix
j = 0
while j < m - i and words1[n - 1 - j] == words2[m - 1 - j]:
j += 1
return i + j == m
class Solution {
public:
bool areSentencesSimilar(string sentence1, string sentence2) {
vector<string> words1 = split(sentence1);
vector<string> words2 = split(sentence2);
if (words1.size() < words2.size()) swap(words1, words2);
int n = words1.size(), m = words2.size();
int i = 0;
while (i < m && words1[i] == words2[i]) ++i;
int j = 0;
while (j < m - i && words1[n - 1 - j] == words2[m - 1 - j]) ++j;
return i + j == m;
}
private:
vector<string> split(const string& s) {
vector<string> res;
istringstream iss(s);
string word;
while (iss >> word) res.push_back(word);
return res;
}
};
class Solution {
public boolean areSentencesSimilar(String sentence1, String sentence2) {
String[] words1 = sentence1.split(" ");
String[] words2 = sentence2.split(" ");
if (words1.length < words2.length) {
String[] temp = words1; words1 = words2; words2 = temp;
}
int n = words1.length, m = words2.length;
int i = 0;
while (i < m && words1[i].equals(words2[i])) i++;
int j = 0;
while (j < m - i && words1[n - 1 - j].equals(words2[m - 1 - j])) j++;
return i + j == m;
}
}
var areSentencesSimilar = function(sentence1, sentence2) {
let words1 = sentence1.split(' ');
let words2 = sentence2.split(' ');
if (words1.length < words2.length) {
[words1, words2] = [words2, words1];
}
let n = words1.length, m = words2.length;
let i = 0;
while (i < m && words1[i] === words2[i]) i++;
let j = 0;
while (j < m - i && words1[n - 1 - j] === words2[m - 1 - j]) j++;
return i + j === m;
};
The Sentence Similarity III problem boils down to checking if the shorter sentence appears as a prefix and/or suffix of the longer one, in order. By using two pointers to scan for matching words at the start and end, we can efficiently determine similarity in linear time. This approach avoids brute-force insertions, leverages the constraints, and provides a clean, elegant solution.