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

# Validate IP Address Python Solution with Tests

> Tested Python solution for LeetCode 468 with 25 pytest cases. Generate a practice environment with lcpy.

LeetCode 468, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/validate-ip-address/description/).

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

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

## Problem

Given a string `queryIP`, return `"IPv4"` if IP is a valid IPv4 address, `"IPv6"` if IP is a valid IPv6 address or `"Neither"` if IP is not a correct IP of any type.

A valid **IPv4** address is an IP in the form `"x1.x2.x3.x4"` where `0 <= xi <= 255` and `xi` **cannot contain** leading zeros. For example, `"192.168.1.1"` and `"192.168.1.0"` are valid IPv4 addresses while `"192.168.01.1"`, `"192.168.1.00"`, and `"192.168@1.1"` are invalid IPv4 addresses.

A valid **IPv6** address is an IP in the form `"x1:x2:x3:x4:x5:x6:x7:x8"` where:

* `1 <= xi.length <= 4`
* `xi` is a **hexadecimal string** which may contain digits, lowercase English letters (`'a'` to `'f'`) and upper-case English letters (`'A'` to `'F'`).
* Leading zeros are allowed in `xi`.

For example, `"2001:0db8:85a3:0000:0000:8a2e:0370:7334"` and `"2001:db8:85a3:0:0:8A2E:0370:7334"` are valid IPv6 addresses, while `"2001:0db8:85a3::8A2E:037j:7334"` and `"02001:0db8:85a3:0000:0000:8a2e:0370:7334"` are invalid IPv6 addresses.

### Examples

```
Input: queryIP = "172.16.254.1"
Output: "IPv4"
```

**Explanation:** This is a valid IPv4 address, return "IPv4".

```
Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
```

**Explanation:** This is a valid IPv6 address, return "IPv6".

```
Input: queryIP = "256.256.256.256"
Output: "Neither"
```

**Explanation:** This is neither a IPv4 address nor a IPv6 address.

### Constraints

* 1 \<= queryIP.length \<= 50
* queryIP consists only of English letters, digits and the characters '.' and ':'.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n) for the split parts
    def valid_ip_address(self, query_ip: str) -> str:
        if self._is_ipv4(query_ip):
            return "IPv4"
        if self._is_ipv6(query_ip):
            return "IPv6"
        return "Neither"

    def _is_ipv4(self, query_ip: str) -> bool:
        parts = query_ip.split(".")
        if len(parts) != 4:
            return False
        return all(self._is_ipv4_octet(part) for part in parts)

    def _is_ipv4_octet(self, part: str) -> bool:
        if not part or len(part) > 3 or not part.isdigit():
            return False
        if part[0] == "0" and len(part) > 1:
            return False
        return int(part) <= 255

    def _is_ipv6(self, query_ip: str) -> bool:
        parts = query_ip.split(":")
        if len(parts) != 8:
            return False
        return all(self._is_ipv6_group(part) for part in parts)

    def _is_ipv6_group(self, part: str) -> bool:
        if not 1 <= len(part) <= 4:
            return False
        return all(char in self.HEX_DIGITS for char in part)

    HEX_DIGITS: frozenset[str] = frozenset("0123456789abcdefABCDEF")
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) for the split parts |

## Tags


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