My 42s Python Solution

  • 1
     def validIPAddress(self, IP):
            def is_valid_IPV4(ip):
                parts = ip.split(".")
                if len(parts)!= 4:
                    return False
                for p in parts:
                    if p.isdigit() == False or p != str(int(p)) or int(p)<0 or int(p)>255:
                        return False
                return "IPv4"
            def is_valid_IPV6(ip):
                parts = ip.split(":")
                if len(parts) != 8:
                    return False
                for p in parts:
                    if len(p)>4 or len(p)==0:
                        return False
                    for char in p:
                        if char not in string.hexdigits:
                            return False
                return "IPv6"
            return is_valid_IPV4(IP) or is_valid_IPV6(IP) or "Neither"

Log in to reply

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