Share My Java Solution with Comments


  • 1
    S
    public class Solution {
        public String validIPAddress(String IP) {
            if (IP.contains(".") && isIpv4(IP)) {
                return "IPv4";
            } else if (IP.contains(":") && isIpv6(IP)) {
                return "IPv6";
            } else {
                return "Neither";
            }
        }
        
        public boolean isIpv4(String IP) {
            // count number of "."
            int count = 0;
            for (int i = 0; i < IP.length(); i++) {
                if (IP.charAt(i) == '.') {
                    count++;
                }
                // there is no empty string between "." and number of "." cannot be greater than 3
                if ((i > 0 && IP.charAt(i) == '.' && IP.charAt(i - 1) == '.') || count > 3) {
                    return false;
                }
            }
            // number of "." cannot be less than 3
            if (count < 3) {
                return false;
            }
            String[] strs = IP.split("\\.");
            for (String str : strs) {
                // str cannot has a number with size greater than 3 and leading zeroes
                if (str.length() > 3 || (str.length() > 1 && str.charAt(0) == '0')) {
                    return false;
                }
                // check if each element is digit
                for (int i = 0; i < str.length(); i++) {
                    if (!Character.isDigit(str.charAt(i))) {
                        return false;
                    }
                }
                // check the number range
                int num = Integer.parseInt(str);
                if (num < 0 || num > 255) {
                    return false;
                }
            }
            return true;
        }
        
        public boolean isIpv6(String IP) {
            // count number of ":"
            int count = 0;
            for (int i = 0; i < IP.length(); i++) {
                if (IP.charAt(i) == ':') {
                    count++;
                }
                // there is no empty string between ":" and number of ":" cannot be greater than 7
                if ((i > 0 && IP.charAt(i) == ':' && IP.charAt(i - 1) == ':') || count > 7) {
                    return false;
                }
            }
            // number of ":" cannot be less than 7
            if (count < 7) {
                return false;
            }
            String[] strs = IP.split("\\:");
            for (String str : strs) {
                str = str.toLowerCase();
                if (str.equals("") || str.length() > 4) {
                    return false;
                }
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    // check if each element is legal
                    if ('a' <= c && c <= 'f') {
                        continue;
                    }
                    if ('0' <= c && c <= '9') {
                        continue;
                    }
                    return false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.