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

# Substring with Concatenation of All Words

> Tested Python solution for LeetCode 30 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 30, [Hard](/catalog/hard). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/).

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

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

## Problem

You are given a string `s` and an array of strings `words`. All the strings of `words` are of **the same length**.

A **concatenated string** is a string that exactly contains all the strings of any permutation of `words` concatenated.

* For example, if `words = ["ab","cd","ef"]`, then `"abcdef"`, `"abefcd"`, `"cdabef"`, `"cdefab"`, `"efabcd"`, and `"efcdab"` are all concatenated strings. `"acdbef"` is not a concatenated string because it is not the concatenation of any permutation of `words`.

### Examples

```
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
```

**Explanation:**
The substring starting at 0 is "barfoo". It is the concatenation of \["bar","foo"] which is a permutation of `words`.
The substring starting at 9 is "foobar". It is the concatenation of \["foo","bar"] which is a permutation of `words`.

```
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []
```

**Explanation:**
There is no concatenated substring.

```
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]
```

**Explanation:**
The substring starting at 6 is "foobarthe". It is the concatenation of \["foo","bar","the"].
The substring starting at 9 is "barthefoo". It is the concatenation of \["bar","the","foo"].
The substring starting at 12 is "thefoobar". It is the concatenation of \["the","foo","bar"].

### Constraints

* 1 \<= s.length \<= 10^4
* 1 \<= words.length \<= 5000
* 1 \<= words\[i].length \<= 30
* s and words\[i] consist of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(word_len * len(s)) - each index enters the window at most once per offset
    # Space: O(len(words)) for the two counters
    def find_substring(self, s: str, words: list[str]) -> list[int]:
        if not s or not words:
            return []

        word_len = len(words[0])
        word_total = len(words)
        concat_len = word_len * word_total
        target = Counter(words)
        result: list[int] = []

        for offset in range(word_len):
            left = offset
            window: Counter[str] = Counter()
            for right in range(offset, len(s) - word_len + 1, word_len):
                word = s[right : right + word_len]
                window[word] += 1
                while window[word] > target[word]:
                    window[s[left : left + word_len]] -= 1
                    left += word_len
                if right + word_len - left == concat_len:
                    result.append(left)

        return result
```

## Complexity

| Time | Space |
| - | - |
| O(word\_len \* len(s)) - each index enters the window at most once per offset | O(len(words)) for the two counters |

## Tags


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