Ip address Validation

XXX. IP Address Validation

String
Validation

Problem Statement:

Write a program to validate whether a given string is a valid IPv4 or IPv6 address.

  • IPv4 uses decimal values with dots (e.g., "172.16.254.1")
  • IPv6 uses hexadecimal values with colons (e.g., "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
  • Return "IPv4", "IPv6", or "Neither"

Implementation:


public String validIPAddress(String IP) {
    if(isValidIPv4(IP)) return "IPv4";
    else if(isValidIPv6(IP)) return "IPv6";
    else return "Neither";
}

// IPv4 Validation
public boolean isValidIPv4(String ip) {
    if(ip.length() < 7) return false;
    if(ip.charAt(0) == '.') return false;
    if(ip.charAt(ip.length()-1) == '.') return false;
    String[] tokens = ip.split("\\.");
    if(tokens.length != 4) return false;
    for(String token : tokens) {
        if(!isValidIPv4Token(token)) return false;
    }
    return true;
}

public boolean isValidIPv4Token(String token) {
    if(token.startsWith("0") && token.length() > 1) return false;
    try {
        int parsedInt = Integer.parseInt(token);
        if(parsedInt < 0 || parsedInt > 255) return false;
        if(parsedInt == 0 && token.charAt(0) != '0') return false;
    } catch(NumberFormatException nfe) {
        return false;
    }
    return true;
}

// IPv6 Validation
public boolean isValidIPv6(String ip) {
    if(ip.length() < 15) return false;
    if(ip.charAt(0) == ':') return false;
    if(ip.charAt(ip.length()-1) == ':') return false;
    String[] tokens = ip.split(":");
    if(tokens.length != 8) return false;
    for(String token : tokens) {
        if(!isValidIPv6Token(token)) return false;
    }
    return true;
}

public boolean isValidIPv6Token(String token) {
    if(token.length() > 4 || token.length() == 0) return false;
    char[] chars = token.toCharArray();
    for(char c : chars) {
        boolean isDigit = c >= 48 && c <= 57;
        boolean isUppercaseAF = c >= 65 && c <= 70;
        boolean isLowerCaseAF = c >= 97 && c <= 102;
        if(!(isDigit || isUppercaseAF || isLowerCaseAF)) 
            return false;
    }
    return true;
}

Complexity:

Time Complexity: O(n), where n is string length
Space Complexity: O(n) for split operation

Key Validation Rules:

  • **IPv4 Rules**:
    • Must have exactly 4 numbers separated by dots
    • Each number between 0 and 255
    • No leading zeros unless number is 0
    • No empty sections
  • **IPv6 Rules**:
    • Must have exactly 8 sections separated by colons
    • Each section 1-4 hexadecimal digits
    • Both uppercase and lowercase letters valid (A-F/a-f)
    • No empty sections

Implementation Notes:

  • **String Processing**:
    • Split strings using dots/colons as delimiters
    • Check for leading/trailing delimiters
    • Validate each section separately
  • **Error Handling**:
    • Use try-catch for IPv4 number parsing
    • Check length constraints before detailed validation
    • Handle edge cases like leading zeros
Previous
Previous

Find In order successor II [parent pointer]

Next
Next

Minimum Moves to equal array elements [Math]