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

# Restore IP Addresses Python Solution

> Tested Python solution for LeetCode 93 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 93, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/restore-ip-addresses/description/).

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

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

## Problem

A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros.

* For example, `"0.1.2.201"` and `"192.168.1.1"` are **valid** IP addresses, but `"0.011.255.245"`, `"192.168.1.312"` and `"192.168@1.1"` are **invalid** IP addresses.

Given a string `s` containing only digits, return *all possible valid IP addresses that can be formed by inserting dots into* `s`. You are **not** allowed to reorder or remove any digits in `s`. You may return the valid IP addresses in **any** order.

### Examples

```
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]
```

```
Input: s = "0000"
Output: ["0.0.0.0"]
```

```
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
```

### Constraints

* `1 <= s.length <= 20`
* `s` consists of digits only.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)
    # Space: O(1)
    def restore_ip_addresses(self, s: str) -> list[str]:
        results: list[str] = []

        def is_valid_part(part: str) -> bool:
            if len(part) > 1 and part[0] == "0":
                return False
            return int(part) <= 255

        def backtrack(start: int, parts: list[str]) -> None:
            if len(parts) == 4:
                if start == len(s):
                    results.append(".".join(parts))
                return
            remaining_digits = len(s) - start
            remaining_parts = 4 - len(parts)
            if remaining_digits < remaining_parts or remaining_digits > remaining_parts * 3:
                return
            for length in range(1, 4):
                if start + length > len(s):
                    break
                part = s[start : start + length]
                if is_valid_part(part):
                    parts.append(part)
                    backtrack(start + length, parts)
                    parts.pop()

        backtrack(0, [])
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(1) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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