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

# Permutation in String Python Solution

> Tested Python solution for LeetCode 567 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 567, Medium. Topics: Hash Table, Two Pointers, String, Sliding Window. [View on LeetCode](https://leetcode.com/problems/permutation-in-string/description/).

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

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

## Problem

Given two strings `s1` and `s2`, return `true` if `s2` contains a permutation of `s1`, or `false` otherwise.

In other words, return `true` if one of `s1`'s permutations is the substring of `s2`.

### Examples

```
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
```

```
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
```

### Constraints

* 1 \<= s1.length, s2.length \<= 10^4
* s1 and s2 consist of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(s2))
    # Space: O(1) - only 26 letters
    def check_inclusion(self, s1: str, s2: str) -> bool:
        len1, len2 = len(s1), len(s2)
        if len1 > len2:
            return False

        s1_count = [0] * 26
        window_count = [0] * 26

        for c in s1:
            s1_count[ord(c) - ord("a")] += 1

        for i in range(len2):
            window_count[ord(s2[i]) - ord("a")] += 1
            if i >= len1:
                window_count[ord(s2[i - len1]) - ord("a")] -= 1
            if i >= len1 - 1 and window_count == s1_count:
                return True

        return False
```

## Complexity

| Time       | Space                  |
| ---------- | ---------------------- |
| O(len(s2)) | O(1) - only 26 letters |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
