Validate IP Address
Problem
Given a string queryIP, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, or "Neither" otherwise. IPv4 needs four decimal parts 0–255 with no leading zeros (except "0"). IPv6 needs eight hexadecimal parts of length 1–4.
queryIP = "172.16.254.1""IPv4"def valid_ip_address(q):
if q.count('.') == 3:
parts = q.split('.')
for p in parts:
if not p or len(p) > 3 or not p.isdigit(): return "Neither"
if p[0] == '0' and len(p) > 1: return "Neither"
if int(p) > 255: return "Neither"
return "IPv4"
if q.count(':') == 7:
parts = q.split(':')
hexd = set("0123456789abcdefABCDEF")
for p in parts:
if not (1 <= len(p) <= 4): return "Neither"
if any(c not in hexd for c in p): return "Neither"
return "IPv6"
return "Neither"
function validIPAddress(q) {
if ((q.match(/\./g) || []).length === 3) {
const parts = q.split('.');
for (const p of parts) {
if (!p || p.length > 3 || !/^\d+$/.test(p)) return "Neither";
if (p[0] === '0' && p.length > 1) return "Neither";
if (Number(p) > 255) return "Neither";
}
return "IPv4";
}
if ((q.match(/:/g) || []).length === 7) {
const parts = q.split(':');
for (const p of parts) {
if (p.length < 1 || p.length > 4) return "Neither";
if (!/^[0-9a-fA-F]+$/.test(p)) return "Neither";
}
return "IPv6";
}
return "Neither";
}
class Solution {
public String validIPAddress(String q) {
if (q.chars().filter(c -> c == '.').count() == 3) {
String[] parts = q.split("\\.", -1);
if (parts.length != 4) return "Neither";
for (String p : parts) {
if (p.isEmpty() || p.length() > 3) return "Neither";
for (char c : p.toCharArray()) if (!Character.isDigit(c)) return "Neither";
if (p.charAt(0) == '0' && p.length() > 1) return "Neither";
if (Integer.parseInt(p) > 255) return "Neither";
}
return "IPv4";
}
if (q.chars().filter(c -> c == ':').count() == 7) {
String[] parts = q.split(":", -1);
if (parts.length != 8) return "Neither";
for (String p : parts) {
if (p.length() < 1 || p.length() > 4) return "Neither";
for (char c : p.toCharArray())
if (!(Character.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
return "Neither";
}
return "IPv6";
}
return "Neither";
}
}
string validIPAddress(string q) {
if (count(q.begin(), q.end(), '.') == 3) {
stringstream ss(q); string p;
vector<string> parts;
while (getline(ss, p, '.')) parts.push_back(p);
if (parts.size() != 4 || q.back() == '.') return "Neither";
for (auto& t : parts) {
if (t.empty() || t.size() > 3) return "Neither";
for (char c : t) if (!isdigit((unsigned char)c)) return "Neither";
if (t[0] == '0' && t.size() > 1) return "Neither";
if (stoi(t) > 255) return "Neither";
}
return "IPv4";
}
if (count(q.begin(), q.end(), ':') == 7) {
stringstream ss(q); string p;
vector<string> parts;
while (getline(ss, p, ':')) parts.push_back(p);
if (parts.size() != 8) return "Neither";
for (auto& t : parts) {
if (t.size() < 1 || t.size() > 4) return "Neither";
for (char c : t)
if (!(isdigit((unsigned char)c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
return "Neither";
}
return "IPv6";
}
return "Neither";
}
Explanation
An address is either IPv4 or IPv6, and the separator tells them apart: IPv4 uses dots, IPv6 uses colons. So we first count the separators to pick which set of rules to apply, then validate each piece.
If the query has exactly 3 dots, we split on . and check all four parts: each must be all digits, length 1–3, no leading zero (so "01" is rejected), and the value must be at most 255. If every part passes, it is "IPv4".
If instead it has exactly 7 colons, we split on : and check all eight parts: each must be 1 to 4 characters and contain only hexadecimal digits 0-9, a-f, A-F. If they all pass, it is "IPv6".
Any single bad part makes the whole thing "Neither", and so does the wrong number of separators. Checking the separator count first is what keeps the two formats from being confused.
Example: "172.16.254.1" has 3 dots, so we test the four groups 172, 16, 254, 1 — all digits, no leading zeros, and each ≤ 255 — so the answer is "IPv4".