Simple to understand Solution in Python


  • 0
    A

    class Solution(object):
    def validIPAddress(self, IP):
    """
    :type IP: str
    :rtype: str
    """
    if IP.count('.') > 0:
    l = IP.split('.')
    if len(l) != 4:
    return "Neither"
    for x in l:
    try:
    if int(x) < 0 or int(x) > 255 or x.count('-'):
    return "Neither"
    elif len(x) >1 and x[0] == '0':
    return "Neither"
    except ValueError:
    return "Neither"
    return "IPv4"
    else:
    IP = IP.lower()
    l = IP.split(':')
    if len(l) != 8:
    return "Neither"
    for ip in l:
    if len(ip) == 0 or len(ip) > 4:
    return "Neither"
    try:
    if int(ip,16) < 0 or ip.count('-'):
    return "Neither"
    except ValueError:
    return "Neither"
    return "IPv6"


Log in to reply
 

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