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 "[email protected]"
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'
).- 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
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.
Solution
Method 1 - String
To determine whether a given string queryIP
is a valid IPv4 address, a valid IPv6 address, or neither, we need to implement the following checks:
Check for IPv4:
- An IPv4 address consists of four decimal numbers separated by dots.
- Each section must be between 0 and 255.
- No leading zeros are allowed, except for the number 0 itself.
Check for IPv6:
- An IPv6 address consists of eight groups of four hexadecimal digits separated by colons.
- Each group must be between 1 and 4 characters long.
- Leading zeros are allowed.
- Valid characters include digits (0-9) and letters (a-f, A-F).
Approach
- Identify and Split:
- Use the presence of dots (.) and colons (:) to differentiate between IPv4 and IPv6.
- Split the string based on dots for IPv4 and colons for IPv6 to check the sections.
- Validation Functions:
- Create helper functions to validate each section of IPv4 and IPv6 addresses.
- Return Result:
- Return “IPv4” if valid IPv4 address, “IPv6” if valid IPv6 address, and “Neither” if none.
Code
Java
public class Solution {
public String validIPAddress(String queryIP) {
if (queryIP.chars().filter(ch -> ch == '.').count() == 3) {
return validateIPv4(queryIP);
} else if (queryIP.chars().filter(ch -> ch == ':').count() == 7) {
return validateIPv6(queryIP);
} else {
return "Neither";
}
}
private String validateIPv4(String IP) {
String[] parts = IP.split("\\.", -1);
if (parts.length != 4) return "Neither";
for (String part : parts) {
if (part.length() == 0 || part.length() > 3) return "Neither";
if (part.charAt(0) == '0' && part.length() != 1) return "Neither";
for (char ch : part.toCharArray()) {
if (!Character.isDigit(ch)) return "Neither";
}
if (Integer.parseInt(part) > 255) return "Neither";
}
return "IPv4";
}
private String validateIPv6(String IP) {
String[] parts = IP.split(":", -1);
if (parts.length != 8) return "Neither";
String hexDigits = "0123456789abcdefABCDEF";
for (String part : parts) {
if (part.length() == 0 || part.length() > 4) return "Neither";
for (char ch : part.toCharArray()) {
if (hexDigits.indexOf(ch) == -1) return "Neither";
}
}
return "IPv6";
}
}
Python
class Solution:
def validIPAddress(self, queryIP: str) -> str:
if queryIP.count('.') == 3:
return self.validateIPv4(queryIP)
elif queryIP.count(':') == 7:
return self.validateIPv6(queryIP)
else:
return "Neither"
def validateIPv4(self, IP: str) -> str:
parts = IP.split(".")
if len(parts) != 4:
return "Neither"
for part in parts:
if len(part) == 0 or len(part) > 3:
return "Neither"
if part[0] == '0' and len(part) != 1:
return "Neither"
if not part.isdigit() or int(part) > 255:
return "Neither"
return "IPv4"
def validateIPv6(self, IP: str) -> str:
parts = IP.split(":")
if len(parts) != 8:
return "Neither"
hex_digits = "0123456789abcdefABCDEF"
for part in parts:
if len(part) == 0 or len(part) > 4:
return "Neither"
for ch in part:
if ch not in hex_digits:
return "Neither"
return "IPv6"
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the length of the input string. Each character is processed once. - 🧺 Space complexity:
O(1)
, excluding the space required for storing result strings.