The Indian Engineer

Problem 1813 Sentence Similarity III

Posted on 6 mins

Array Two-Pointers String

Problem Statement

Link - Problem 1813

Question

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,

Given two sentences sentence1 and sentence2, return true if sentence1 and sentence2 are similar. Otherwise, return false.

Example 1:

Input: sentence1 = "My name is Haley", sentence2 = "My Haley"

Output: true

Explanation:

sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".

Example 2:

Input: sentence1 = "of", sentence2 = "A lot of words"

Output: false

Explanation:

No single sentence can be inserted inside one of the sentences to make it equal to the other.

Example 3:

Input: sentence1 = "Eating right now", sentence2 = "Eating"

Output: true

Explanation:

sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.

Constraints:

Solution

class Solution {
public:
    vector<string> toStringVec(string& s){
        stringstream ss(s);
        vector<string> ans;
        string w;
        while (ss>>w)
            ans.push_back(w);
        ss.clear();
        return ans;
    }

    bool areSentencesSimilar(string sentence1, string sentence2) {
        auto s1=toStringVec(sentence1), s2=toStringVec(sentence2);
        int n1=s1.size(), n2=s2.size();
        if (n1>n2)
        {
            swap(n1, n2);
            swap(s1, s2);
        }

        int l=0, r2=n2-1, r1=n1-1;

        while(l<n1 && s1[l]==s2[l])
            l++;

        while(r1>=0 && s1[r1]==s2[r2]){
            r1--;
            r2--;
        }

        return r1<l;
    }
};

Complexity Analysis

| Algorithm                                   | Time Complexity | Space Complexity |
| ------------------------------------------- | --------------- | ---------------- |
| Two-pointer traversal with string streaming | O(n)            | O(n)             |

Explanation

Intial Thoughts

The problem can be tackled by breaking down the sentences into individual words and comparing them. The key is to find a way to make the two sentences equal by inserting a sentence into one of them. This can be visualized as finding a common prefix and suffix in the two sentences and checking if the remaining parts can be combined to form a valid sentence. Another approach is to think of it as a string matching problem where we need to find the longest common subsequence between the two sentences.

Intuitive Analysis

To intuitively solve this problem, think of it as trying to fit one sentence into the other by inserting a sentence in between. Start by comparing the two sentences from the beginning and end, and try to find the common words. If the common words are found, check if the remaining parts can be combined to form a valid sentence. Another way to think about it is to consider the words as blocks, and try to fit these blocks together to form a complete sentence. By using this block-fitting approach, we can determine if the two sentences are similar or not.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

One way to look at it is to find one sentence as a concatenation of a prefix and suffix from the other sentence.

Get the longest common prefix between them and the longest common suffix.