Simple, easy to understand with comments


  • 1
    M

    Simple logic: check for . or : and then check respective category (IPv4/IPv6). For each category, split the parts and try to parse out integer values using Integer.parseInt, base). Also had corner case checks for the parts.

    public class Solution {
        public String validIPAddress(String IP) {
            if(IP==null || IP.length()==0) return "Neither";
            if(IP.indexOf(".")>=0)
                return validIP4(IP);
            if(IP.indexOf(":")>=0)
               return validIP6(IP);
            return  "Neither";
        }
        
        // simple logic, split the parts, then parse integer out of the parts
        private String validIP4(String IP){
            // IP4 cannot have :
            if(IP.indexOf(":") >=0)  return "Neither";
            
            // must have exactly 3 dots
            int dotCount = IP.length() - IP.replace(".", "").length();
            if(dotCount != 3) return "Neither";
            
            // try to parse each part
            String[] parts = IP.split("\\.");
            if(parts.length != 4) return "Neither";
            for(String part: parts){
                if(part.length()==0)  return "Neither";
                // since we are parsing integer out of prt, need to invalidate +- separately
                if(part.charAt(0) == '-')  return "Neither";
                if(part.charAt(0) == '+')  return "Neither";
                if(part.length()>1 && part.charAt(0) == '0')  return "Neither";  // for IP4, cannot have leading zero except its single 0
                try{
                    int val = Integer.parseInt(part);
                    if(val<0 || val>255)  return "Neither";
                } catch (Exception e) {
                     return "Neither";
                }
            }
            return "IPv4";
        }
        
        // simple logic, split the parts, then parse integer out of the parts
        private String validIP6(String IP){
            // IP6 cannot have .
            if(IP.indexOf(".") >=0)  return "Neither";
            // must have exactly 7 colons
            int colonCount = IP.length() - IP.replace(":", "").length();
            if(colonCount != 7) return "Neither";
            
            String[] parts = IP.split(":");
            if(parts.length != 8) return "Neither";
            for(String part: parts){
                if(part.length()==0)  return "Neither";
                // since we are parsing integer out of prt, need to invalidate +- separately
                if(part.charAt(0) == '-')  return "Neither";
                if(part.charAt(0) == '+')  return "Neither";  
                if(part.length() >4 )  return "Neither";   // cannot have more than 4 chars
                try{
                    int val = Integer.parseInt(part, 16);
                } catch (Exception e) {
                     return "Neither";
                }
            }
            return "IPv6";
        }
        // debug prints
        private void sop(String s){ System.out.println(s);}
    }
    
    

    '''


Log in to reply
 

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