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 letter ('a'
to 'f'
) and upper-case English letters ('A'
to 'F'
).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.
Example 1:
Input: queryIP = "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4".
Example 2:
Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6".
Example 3:
Input: queryIP = "256.256.256.256" Output: "Neither" Explanation: This is neither a IPv4 address nor a IPv6 address.
Constraints:
queryIP
consists only of English letters, digits and the characters '.'
and ':'
.This problem asks us to validate if a given string represents a valid IPv4 or IPv6 address. The solution uses a simulation approach, creating separate functions to validate each IP type.
Approach:
The solution cleverly separates the validation logic into two distinct functions: isIPv4
and isIPv6
. This improves readability and maintainability.
isIPv4(s)
:
.
), which is invalid for IPv4..
) into four parts. If the resulting array doesn't have exactly four elements, it's not a valid IPv4.convert
in Java/C++/Go/TypeScript, direct conversion in Python/Rust) to safely convert the string to an integer and handle potential overflow.true
; otherwise, false
.isIPv6(s)
:
isxdigit
in C++ or checks against a valid character set in other languages.true
if all parts pass, and false
otherwise.Main function validIPAddress(queryIP)
:
The main function simply calls isIPv4
and isIPv6
sequentially. If either function returns true
, the corresponding IP type is returned; otherwise, "Neither" is returned.
Time Complexity: O(n), where n is the length of the input string. The splitting and validation processes iterate through the string linearly.
Space Complexity: O(n) in the worst case. This is due to the temporary arrays created during the splitting process. In the best case, the space complexity is O(1).
Code Examples (Python):
class Solution:
def validIPAddress(self, queryIP: str) -> str:
def is_ipv4(s: str) -> bool:
ss = s.split(".")
if len(ss) != 4:
return False
for t in ss:
if len(t) > 1 and t[0] == "0":
return False
try:
if not 0 <= int(t) <= 255:
return False
except ValueError:
return False
return True
def is_ipv6(s: str) -> bool:
ss = s.split(":")
if len(ss) != 8:
return False
for t in ss:
if not 1 <= len(t) <= 4:
return False
if not all(c in "0123456789abcdefABCDEF" for c in t):
return False
return True
if is_ipv4(queryIP):
return "IPv4"
if is_ipv6(queryIP):
return "IPv6"
return "Neither"
This Python code efficiently implements the algorithm described above. The try-except
block handles potential ValueError
during integer conversion. Other language implementations follow a similar structure, adapting to the nuances of their respective syntax and libraries.