101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Two Pointers (Prefix and Suffix Matching) - Complex approach
  2. Subsequence Checking - Complex approach

Approach 1: Two Pointers (Prefix and Suffix Matching)

Let's start by understanding what makes two sentences similar according to the problem definition. Two sentences are similar if we can insert a sequence of words into one sentence to make it equal to the other.

Thinking Process: If we can insert words into one sentence to make it equal to the other, then the shorter sentence must be a subsequence of the longer one, with the constraint that the words we insert must be consecutive in the longer sentence.

This means that the shorter sentence can be split into two parts (possibly empty): a prefix and a suffix. The prefix matches the beginning of the longer sentence, and the suffix matches the end of the longer sentence. The words in between the prefix and suffix in the longer sentence are the ones that would be inserted into the shorter sentence.

Intuition: We can use two pointers to match words from both ends of the sentences. We start by matching words from the beginning (prefix) and then from the end (suffix). If we can match all words in the shorter sentence, then the sentences are similar.

Algorithm:

  1. Split both sentences into arrays of words.
  2. Determine which sentence is shorter and which is longer.
  3. Initialize two pointers i and j at the beginning of both arrays.
  4. Match words from the beginning (prefix):
  5. a. While i < shorter.length and j < longer.length and shorter[i] == longer[j], increment both i and j.
  6. Initialize two pointers i2 and j2 at the end of both arrays.
  7. Match words from the end (suffix):
  8. a. While i2 >= i and j2 >= j and shorter[i2] == longer[j2], decrement both i2 and j2.
  9. Check if all words in the shorter sentence have been matched (i > i2).
  10. Return true if all words in the shorter sentence have been matched, false otherwise.

Time Complexity:

O(n)

We process each word in both sentences once, where n is the total number of words in both sentences.

Space Complexity:

O(n)

We store the words of both sentences in arrays, which requires O(n) space.

Approach 2: Subsequence Checking

Another way to approach this problem is to check if one sentence is a subsequence of the other, with the additional constraint that the inserted words must be consecutive.

Thinking Process: If sentence1 is shorter than sentence2, then sentence1 must be a subsequence of sentence2 for them to be similar. Additionally, the words in sentence1 must appear in the same order in sentence2, and the words that are in sentence2 but not in sentence1 must be consecutive.

We can check this by finding all occurrences of words from sentence1 in sentence2, and then verifying that the words in between these occurrences form a single continuous segment.

Intuition: We can use a greedy approach to match words from the shorter sentence with words from the longer sentence. We try to match as many words as possible from the beginning, and then as many words as possible from the end. If all words in the shorter sentence can be matched, then the sentences are similar.

Algorithm:

  1. Split both sentences into arrays of words.
  2. Determine which sentence is shorter and which is longer.
  3. Check if the shorter sentence is a subsequence of the longer one:
  4. a. Initialize pointers i and j at the beginning of both arrays.
  5. b. While i < shorter.length and j < longer.length:
  6. i. If shorter[i] == longer[j], increment i.
  7. ii. Increment j.
  8. c. If i < shorter.length, return false (not all words in shorter were matched).
  9. Check if the unmatched words in the longer sentence form a single continuous segment:
  10. a. Initialize a counter for the number of segments.
  11. b. Iterate through the longer sentence and mark words that match with the shorter sentence.
  12. c. Count the number of transitions from matched to unmatched words.
  13. d. If there is at most one segment of unmatched words, return true.
  14. Return false if there are multiple segments of unmatched words.

Time Complexity:

O(n)

We process each word in both sentences once, where n is the total number of words in both sentences.

Space Complexity:

O(n)

We store the words of both sentences in arrays, which requires O(n) space.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function areSentencesSimilar(sentence1, sentence2):
words1 = split sentence1 by space
words2 = split sentence2 by space
// Determine which sentence is shorter
if length of words1 > length of words2:
swap words1 and words2
// Now words1 is the shorter array and words2 is the longer array
n1 = length of words1
n2 = length of words2
// Match prefix
i = 0
while i < n1 and i < n2 and words1[i] == words2[i]:
i++
// Match suffix
j = 0
while j < n1 - i and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
j++
// Check if all words in the shorter sentence have been matched
return i + j >= n1
ProblemSolutionCode
101 Logo
onenoughtone