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

# String Matching in an Array Python Solution

> Tested Python solution for LeetCode 1408 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1408, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [String Matching](/catalog/topics/string-matching). [View on LeetCode](https://leetcode.com/problems/string-matching-in-an-array/description/).

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

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

## Problem

Given an array of string `words`, return all strings in `words` that are a substring of another word. You can return the answer in **any order**.

### Examples

```
Input: words = ["mass","as","hero","superhero"]
Output: ["as","hero"]
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
["hero","as"] is also a valid answer.
```

```
Input: words = ["leetcode","et","code"]
Output: ["et","code"]
Explanation: "et", "code" are substring of "leetcode".
```

```
Input: words = ["blue","green","bu"]
Output: []
Explanation: No string of words is substring of another string.
```

### Constraints

* `1 <= words.length <= 100`
* `1 <= words[i].length <= 30`
* `words[i]` contains only lowercase English letters.
* All the strings of `words` are unique.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * m) where n = len(words), m = max word length
    # Space: O(1) excluding the output list
    def string_matching(self, words: list[str]) -> list[str]:
        result: list[str] = []
        for i, word in enumerate(words):
            for j, other in enumerate(words):
                if i != j and word in other:
                    result.append(word)
                    break
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* m) where n = len(words), m = max word length | O(1) excluding the output list |

## Tags

[NeetCode All](/catalog/neetcode).


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