JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

Given a string IP, 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 “x_1.x_2.x_3.x_4” where 0 <= x_i <= 255 and x_i cannot contain leading zeros. For example, “192.168.1.1” and “192.168.1.0” are valid IPv4 addresses but “192.168.01.1”, while “192.168.1.00” and “[email protected]” are invalid IPv4 addresses.

A valid IPv6 address is an IP in the form “x_1:x_2:x_3:x_4:x_5:x_6:x_7:x_8” where:

  • 1 <= x_i.length <= 4
  • x_i is a hexadecimal string which may contain digits, lower-case English letter (‘a’ to ‘f’) and upper-case English letters (‘A’ to ‘F’).
  • Leading zeros are allowed in x_i.

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: IP = "172.16.254.1"
Output: "IPv4"
Explanation: This is a valid IPv4 address, return "IPv4".

Example 2:

Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: This is a valid IPv6 address, return "IPv6".

Example 3:

Input: IP = "256.256.256.256"
Output: "Neither"
Explanation: This is neither a IPv4 address nor a IPv6 address.

Example 4:

Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334:"
Output: "Neither"

Example 5:

Input: IP = "1e1.4.5.6"
Output: "Neither"

Code

class Solution {
    public String validIPAddress(String queryIP) {
        if(isValidIPv4(queryIP)){
            return "IPv4";
        }

        if(isValidIPv6(queryIP)){
            return "IPv6";
        }

        return "Neither";
    }

    private 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;
    }


    private boolean isValidIPv4Token(String token) {
        if(token.startsWith("0") && token.length() > 1){
            return false;
        }

        // 0-255
        try {
            int num = Integer.valueOf(token);
            if(num < 0 || num > 255) return false;
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    private 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;
    }

    private boolean isValidIPv6Token(String token) {
        if(token.length() > 4 || token.length() == 0) return false;

        // 数字, a-f, A-F
        for(char c : token.toCharArray()){
            boolean isDigit = c >= 48 && c <= 57;
            boolean isUppercase = c >= 65 && c <= 70;
            boolean isLowercase = c >= 97 && c <= 102;
            if(!isDigit && !isUppercase && !isLowercase){
                return false;
            }
        }

        return true;
    }
}