Python 91.12%


  • 0
    X
    class Solution(object):
        def validIPAddress(self, IP):
            """
            :type IP: str
            :rtype: str
            """
            hex_digits = set("0123456789abcdefABCDEF")
    
            if len(IP.split('.')) == 4:
                for i in range(4):
                    if IP.split('.')[i].isdigit() == False:
                        return 'Neither'
                    if len(IP.split('.')[i]) != 1 and IP.split('.')[i][0] == '0':
                        return 'Neither'
                    if int(IP.split('.')[i]) > 255:
                        return 'Neither'
                return "IPv4"
    
            num = [] 
    
            if len(IP.split(':')) == 8:
                for char in ''.join(IP.split(':')):
                    if char not in hex_digits:
                        return 'Neither'
                for i in range(8):
                    if len(IP.split(':')[i]) > 4 or len(IP.split(':')[i]) == 0 :
                        return 'Neither'
                    if len(IP.split(':')[i]) != 4:
                        num.append(i)
                if len(num) != 0 and num[-1] != 7:
                    if IP.split(':')[num[-1]+1].isdigit() == False:
                        xx = 0
                        for char in IP.split(':')[num[-1]+1]:
                            if char.isupper() == True:
                                xx += 1
                        if xx == 0:
                            return 'Neither'
                return "IPv6"
            return "Neither"    
    

Log in to reply
 

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