---
title: "LeetCode #93: Restore IP Addresses"
url: https://perrotta.dev/2025/12/leetcode-%2393-restore-ip-addresses/
last_updated: 2026-01-03
---


[LeetCode #93: Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses):

```python
class Solution:
    def restoreIpAddresses(self, s: str) -> List[str]:
        ans = []

        def backtrack(s: str, t: List[str]):
            if not s:
                if t and len(t) == 4:
                    ans.append('.'.join(t))
                    return
                else:
                    return

            if len(t) >= 4:
                return

            def is_valid(chunk):
                return chunk and (chunk == "0" or (not chunk.startswith('0') and 0 <= int(chunk) <= 255))

            for i in [1, 2, 3]:
                if i > len(s):
                    break
                chunk = s[:i]
                if is_valid(chunk):
                    backtrack(s[i:], t + [s[:i]])

        backtrack(s, [])

        return ans
```

