> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sentence Similarity III Python Solution

> Tested Python solution for LeetCode 1813 with 28 pytest cases. Generate a practice environment with lcpy.

LeetCode 1813, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/sentence-similarity-iii/description/).

Generate this problem as a practice environment: tested reference solution, 28 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1813   # by problem number
lcpy gen -s sentence_similarity_iii   # by problem name
```

## Problem

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,

* `s1 = "Hello Jane"` and `s2 = "Hello my name is Jane"` can be made equal by inserting `"my name is"` between `"Hello"` and `"Jane"` in `s1`.
* `s1 = "Frog cool"` and `s2 = "Frogs are cool"` are **not** similar, since although there is a sentence `"s are"` inserted into `s1`, it is not separated from `"Frog"` by a space.

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

### Examples

```
Input: sentence1 = "My name is Haley", sentence2 = "My Haley"
Output: true
```

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

```
Input: sentence1 = "of", sentence2 = "A lot of words"
Output: false
```

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

```
Input: sentence1 = "Eating right now", sentence2 = "Eating"
Output: true
```

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

### 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

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sentence_similarity_iii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sentence_similarity_iii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) where n, m are the sentence lengths
    # Space: O(n + m) for the split word lists
    def are_sentences_similar(self, sentence1: str, sentence2: str) -> bool:
        words1 = sentence1.split()
        words2 = sentence2.split()
        if len(words1) < len(words2):
            words1, words2 = words2, words1

        prefix = 0
        while prefix < len(words2) and words1[prefix] == words2[prefix]:
            prefix += 1

        suffix = 0
        while (
            suffix < len(words2) - prefix
            and words1[len(words1) - 1 - suffix] == words2[len(words2) - 1 - suffix]
        ):
            suffix += 1

        return prefix + suffix == len(words2)
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where n, m are the sentence lengths | O(n + m) for the split word lists |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.