Python solution with comments


  • 1
    L
        def validIPAddress(self, IP):
            IPV4 = IP.split('.')
            if len(IPV4) == 4 : # potential IPV4
                for val in IPV4 :
                    if not val : return "Neither"
                    if val[0] == '-' or val[0] == '0'and len(val) > 1 : return "Neither"
                    try : 
                        val = int(val)
                        if val < 0 or val > 255 : return "Neither"
                    except : return "Neither"
                return "IPv4"
                
            IPV6 = IP.split(':')
            if len(IPV6) == 8 : # potential IPV6
                for val in IPV6 :
                    if not val : return "Neither"
                    if val[0] == '-' or len(val) > 4  : return "Neither"
                    try : int(val, 16)
                    except : return "Neither"
                return "IPv6"
            
            return "Neither" #neitheer IPV4 or IPV6
    

Log in to reply
 

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