Python Solution


  • 8
    class Solution(object):
        def validIPAddress(self, IP):
            def is_hex(s):
                hex_digits = set("0123456789abcdefABCDEF")
                for char in s:
                    if not (char in hex_digits):
                        return False
                return True
            ary = IP.split('.')
            if len(ary) == 4:
                for i in xrange(len(ary)):
                    if not ary[i].isdigit() or not 0 <= int(ary[i]) < 256 or (ary[i][0] == '0' and len(ary[i]) > 1):
                        return "Neither"
                return "IPv4"
            ary = IP.split(':')
            if len(ary) == 8:
                for i in xrange(len(ary)):
                    tmp = ary[i]
                    if len(tmp) == 0 or not len(tmp) <= 4 or not is_hex(tmp):    
                        return "Neither"
                return "IPv6"
            return "Neither"
    

  • 0

    Love the idea of using a set to store hex digits.


  • 1

    @Ipeq1 Actually any valid IP address string has a maximum length. Checking the given string length first might save time for excessively long strings.


  • 0
    W

    I like your compact code.
    Here is my complicated one:

        def validIPAddress(self, IP):
            """
            :type IP: str
            :rtype: str
            """
            a = self.IPv4(IP)
            b = self.IPv6(IP)
            if a:
                return "IPv4"
            if b:
                return "IPv6"
            return "Neither"
        
        def IPv4(self, IP):
            if IP.count('.') == 3:
                res = IP.split('.')
                for string in res:
                    if len(string) > 0 and len(string) <= 4:
                        for s in string:
                            if s not in "0123456789":
                                return False
                        if len(string) > 1 and string[0] == '0' or int(string) >= 256:
                            return False
                    else:
                        return False
                    
                return True
            else:
                return False
        
        def IPv6(self, IP):
            if IP.count(':') == 7:
                res = IP.split(':')
                for string in res:
                    if len(string) > 0 and len(string) <= 4:
                        for s in string:
                            if s not in "0123456789abcdefABCDEF":
                                return False
                    else:
                        return False
                return True
            else:
                return False
    

  • 2

    My solution here https://discuss.leetcode.com/topic/103614

    def validIPAddress(self, IP):
            
            def isIPv4(s):
                try: return str(int(s)) == s and 0 <= int(s) <= 255
                except: return False
                
            def isIPv6(s):
                if len(s) > 4: return False
                try: return int(s, 16) >= 0 and s[0] != '-'
                except: return False
    
            if IP.count(".") == 3 and all(isIPv4(i) for i in IP.split(".")): 
                return "IPv4"
            if IP.count(":") == 7 and all(isIPv6(i) for i in IP.split(":")): 
                return "IPv6"
            return "Neither"

Log in to reply
 

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