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

# Magical String Python Solution with Tests

> Tested Python solution for LeetCode 481 with 33 pytest cases. Generate a practice environment with lcpy.

LeetCode 481, [Medium](/catalog/medium). Topics: [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/magical-string/description/).

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

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

## Problem

A magical string `s` consists of only `'1'` and `'2'` and obeys the following rules:

* Concatenating the sequence of lengths of its consecutive groups of identical characters `'1'` and `'2'` generates the string `s` itself.

The first few elements of `s` is `s = "1221121221221121122......"`. If we group the consecutive 1's and 2's in `s`, it will be `"1 22 11 2 1 22 1 22 11 2 11 22 ......"` and counting the occurrences of 1's or 2's in each group yields the sequence `"1 2 2 1 1 2 1 2 2 1 2 2 ......"`.

You can see that concatenating the occurrence sequence gives us `s` itself.

Given an integer `n`, return the number of 1's in the first n number in the magical string `s`.

### Examples

```
Input: n = 6
Output: 3
Explanation: The first 6 elements of magical string s is "122112" and it contains three 1's, so return 3.
```

```
Input: n = 1
Output: 1
```

### Constraints

* 1 \<= n \<= 10\<sup>5\</sup>

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def magical_string(self, n: int) -> int:
        if n <= 0:
            return 0
        s = [1, 2, 2]
        i = 2
        while len(s) < n:
            nxt = s[-1] ^ 3
            s.extend([nxt] * s[i])
            i += 1
        return s[:n].count(1)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


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