Validate IP Address

medium string

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.

InputqueryIP = "172.16.254.1"
Output"IPv4"
Four valid decimal groups separated by dots.

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";
}
Time: O(n) Space: O(n)