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

# Encode and Decode Strings Python Solution

> Tested Python solution for LeetCode 271 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 271, Medium. Topics: Array, String, Design. [View on LeetCode](https://leetcode.com/problems/encode-and-decode-strings/description/).

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

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

## Problem

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.

### Examples

```
Input: dummy_input = ["Hello","World"]
Output: "Hello,World"
Explanation: Machine 1:
Codec encoder = new Codec();
String msg = encoder.encode(strs);
Machine 1 ---msg---> Machine 2
Machine 2:
Codec decoder = new Codec();
String[] strs = decoder.decode(msg);
```

### Constraints

* 1 \<= strs.length \<= 200
* 0 \<= strs\[i].length \<= 200
* strs\[i] contains any possible characters out of 256 valid ASCII characters.

**Note:**

* The string may contain any possible characters out of 256 valid ASCII characters. Your algorithm should be generalized enough to work on any possible characters.
* Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
* Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def encode(self, strs: list[str]) -> str:
        encoded = ""
        for s in strs:
            # Format: length + '#' + string
            encoded += str(len(s)) + "#" + s
        return encoded

    # Time: O(n)
    # Space: O(n)
    def decode(self, s: str) -> list[str]:
        decoded = []
        i = 0

        while i < len(s):
            # Find the delimiter '#'
            j = i
            while s[j] != "#":
                j += 1

            # Extract length
            length = int(s[i:j])

            # Extract string of that length
            decoded.append(s[j + 1 : j + 1 + length])

            # Move to next encoded string
            i = j + 1 + length

        return decoded
```

## Complexity

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

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
