Formatted question description: https://leetcode.ca/all/1813.html

# 1813. Sentence Similarity III

Medium

## Description

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", "hello world hello world" are all sentences. Words consist of only uppercase and lowercase English letters.

Two sentences sentence1 and sentence2 are 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.

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.

Example 4:

Input: sentence1 = “Luky”, sentence2 = “Lucccky”

Output: false

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.

## Solution

If sentence1 and sentence2 are equal, return true. Otherwise, split sentence1 and sentence2 into two arrays by spaces.

If the two arrays have the same length, then return true if and only if the two arrays are exactly the same.

If the two arrays have different lengths, then check whether it is possible to obtain the longer array by inserting a consecutive part into the shorter array. If so, return true. Otherwise, return false.

class Solution {
public boolean areSentencesSimilar(String sentence1, String sentence2) {
if (sentence1.equals(sentence2))
return true;
String[] array1 = sentence1.split(" ");
String[] array2 = sentence2.split(" ");
int length1 = array1.length, length2 = array2.length;
if (length1 == length2) {
for (int i = 0; i < length1; i++) {
if (!array1[i].equals(array2[i]))
return false;
}
return true;
} else if (length1 < length2)
return areSentencesSimilar(array1, array2);
else
return areSentencesSimilar(array2, array1);
}

public boolean areSentencesSimilar(String[] array1, String[] array2) {
int length1 = array1.length, length2 = array2.length;
int difference = length2 - length1;
int[] indices = new int[length1];
for (int i = 0; i < length1; i++)
indices[i] = i + difference;
for (int i = 0; i <= length1; i++) {
boolean flag = true;
for (int j = 0; j < length1; j++) {
int index = indices[j];
if (!array1[j].equals(array2[index])) {
flag = false;
break;
}
}
if (flag)
return true;
if (i < length1)
indices[i] -= difference;
}
return false;
}
}