25. Sentence Similarity III
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:
Example 2:
Example 3:
Example 4:
Solution:
Approach: Check all the matching values of smaller sentence with larger. If we don't find any matching words from last or first we return false
Time Complexity: O(n)
Last updated