101 Logo
onenoughtone

Problem Statement

Text Insertion Checker

You're developing a text comparison tool that needs to determine if two sentences can be made identical by inserting additional words into one of them.

A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, "Hello World", "HELLO", and "hello world hello world" are all valid sentences. Words consist of only uppercase and lowercase English letters.

Two sentences sentence1 and sentence2 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. For example, sentence1 = "Hello my name is Jane" and sentence2 = "Hello Jane" can be made equal by inserting "my name is" between "Hello" and "Jane" in sentence2.

Your task is to determine if two given sentences are similar according to this definition.

Examples

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.

Example 4:

Input: sentence1 = "Luky", sentence2 = "Lucccky"
Output: false
Explanation: The words in the sentences don't match, so they cannot be made similar through insertion.

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

Problem Breakdown

To solve this problem, we need to:

  1. One sentence must be a subsequence of the other for them to be similar
  2. The words that need to be inserted must be consecutive in the longer sentence
  3. The insertion can happen at the beginning, middle, or end of the shorter sentence
  4. We can check if one sentence is a prefix and suffix of the other
  5. The problem can be solved by comparing words from both ends of the sentences
  6. If the shorter sentence is not a subsequence of the longer one, they cannot be similar
ProblemSolutionCode
101 Logo
onenoughtone