Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.



IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;



Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.



IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).




However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.



Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.



Note:
You may assume there is no extra space or special characters in the input string.


Example 1:

Input: "172.16.254.1"

Output: "IPv4"

Explanation: This is a valid IPv4 address, return "IPv4".




Example 2:

Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"

Output: "IPv6"

Explanation: This is a valid IPv6 address, return "IPv6".



Example 3:

Input: "256.256.256.256"

Output: "Neither"

Explanation: This is neither a IPv4 address nor a IPv6 address.

Difficulty: MEDIUM_HARD
Starter code: class Solution:
    def validIPAddress(self, IP: str) -> str:
        

Solutions:
class Solution:

	def validIPAddress(self, IP):
		if ':' in IP:
			res = self.validIPv6(IP)
			return 'IPv6' if res else 'Neither'
		elif '.' in IP:
			res = self.validIPV4(IP)
			return 'IPv4' if res else 'Neither'
		else:
			return 'Neither'

	def validIPV4(self, IP):
		charSet = set(list('0123456789'))
		parts = IP.split('.')
		if len(parts) != 4:
			return False
		for part in parts:
			if len(part) < 1:
				return False
			for c in part:
				if c not in charSet:
					return False
			if not 0 <= int(part) <= 255:
				return False
			if part[0] == '0' and len(part) > 1:
				return False
		return True

	def validIPv6(self, IP):
		charSet = set(list('0123456789abcdefABCDEF'))
		parts = IP.split(':')
		if len(parts) != 8:
			return False
		zeroFlag = False
		omtFlag = False
		for part in parts:
			if len(part) == 0:
				omtFlag = True
			if self.allZero(part):
				zeroFlag = True
			if len(part) > 4:
				return False
			for c in part:
				if c not in charSet:
					return False
		if zeroFlag and omtFlag:
			return False
		return True

	def allZero(self, s):
		for i in range(len(s)):
			if s[i] != '0':
				return False
		return True


class Solution:

	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'


class Solution:

	def validIPAddress(self, IP: str):
		IP = IP.lower()
		import re
		if '.' in IP:
			ip_parts = IP.split('.')
			if len(ip_parts) == 4:
				for ip_part in ip_parts:
					if ip_part.startswith('0') and ip_part != '0':
						return 'Neither'
					if not (ip_part.isdigit() and 0 <= int(ip_part) <= 255):
						return 'Neither'
				return 'IPv4'
			else:
				return 'Neither'
		elif ':' in IP:
			ip_parts = IP.split(':')
			if len(ip_parts) == 8:
				for ip_part in ip_parts:
					if not re.match('^[0123456789abcdef]{1,4}$', ip_part):
						return 'Neither'
				return 'IPv6'
			else:
				return 'Neither'
		else:
			return 'Neither'


class Solution:

	def isIPv4(self, IP):
		IP = IP.split('.')
		if len(IP) != 4:
			return False
		for ip in IP:
			if not ip.isdigit():
				return False
			if int(ip) > 255 or int(ip) < 0:
				return False
			if str(int(ip)) != ip:
				return False
		return True

	def isIPv6(self, IP):
		IP = IP.split(':')
		if len(IP) != 8:
			return False
		for ip in IP:
			ip = ip.lower()
			for c in ip:
				if (not c >= '0' or not c <= '9') and (not c >= 'a' or not c <= 'f'):
					return False
			if len(ip) > 4 or len(ip) == 0:
				return False
			if int(ip, 16) > 65535 or int(ip, 16) < 0:
				return False
		return True

	def validIPAddress(self, IP):
		(f1, f2) = (0, 0)
		if ':' in IP:
			f1 = 1
		if '.' in IP:
			f2 = 1
		if f1 and f2:
			return 'Neither'
		if self.isIPv4(IP):
			return 'IPv4'
		elif self.isIPv6(IP):
			return 'IPv6'
		else:
			return 'Neither'


class Solution:

	def checkIPv4(self, IP):
		IP = IP.lower().split('.')
		if len(IP) != 4:
			return False
		for block in IP:
			try:
				k = int(block)
				if not 0 <= k <= 255 or (k > 0 and block[0] == '0') or (k == 0 and len(block) > 1):
					return False
			except ValueError:
				return False
		return True

	def checkIPv6(self, IP):
		IP = IP.lower().split(':')
		if len(IP) != 8:
			return False
		for block in IP:
			try:
				k = int('0x' + block, 16)
				if len(block) > 4 or not 0 <= k <= 65535:
					return False
			except ValueError:
				return False
		return True

	def validIPAddress(self, IP):
		if self.checkIPv4(IP):
			return 'IPv4'
		elif self.checkIPv6(IP):
			return 'IPv6'
		else:
			return 'Neither'


class Solution(object):

	def ipv4(self, ip):
		l = ip.split('.')
		if len(l) != 4:
			return False
		for i in l:
			if len(i) > 1 and i[0] == '0':
				return False
			elif i.isdigit() and 0 <= int(i) <= 255:
				pass
			else:
				return False
		return True

	def ipv6(self, ip):
		l = ip.split(':')
		if len(l) != 8:
			return False
		for i in l:
			if len(i) > 4 or len(i) == 0:
				return False
			validset = set('123456789abcdefABCDEF0')
			for j in i:
				if j in validset:
					pass
				else:
					return False
		return True

	def validIPAddress(self, IP):
		if self.ipv4(IP):
			return 'IPv4'
		if self.ipv6(IP):
			return 'IPv6'
		return 'Neither'


class Solution:

	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 range(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 range(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'


class Solution:

	def validIPAddress(self, IP):

		def is_hex(hexs):
			hex_set = set('0123456789abcdefABCDEF')
			return set(hexs) < hex_set
		result = 'Neither'
		if '.' in IP:
			sub_str = IP.split('.')
			if len(sub_str) != 4:
				return result
			result = 'IPv4'
		elif ':' in IP:
			sub_str = IP.split(':')
			if len(sub_str) != 8:
				return result
			result = 'IPv6'
		else:
			return result
		for sub in sub_str:
			if result == 'IPv4':
				if not sub.isdigit():
					result = 'Neither'
					break
				if len(str(int(sub))) != len(sub) or int(sub) > 255:
					result = 'Neither'
					break
			if result == 'IPv6':
				if len(sub) > 4 or len(sub) == 0 or (not is_hex(sub)):
					result = 'Neither'
					break
		return result


class Solution:

	def validIPAddress(self, IP):
		if len(IP) < 4:
			return 'Neither'
		if len(IP.split('.')) == 4:
			for i in IP.split('.'):
				if len(i) == 0:
					return 'Neither'
				elif not i.isdigit():
					return 'Neither'
				elif not 0 <= int(i) <= 255:
					return 'Neither'
				elif (i[0] == '0') & (len(i) > 1):
					return 'Neither'
			return 'IPv4'
		elif (len(IP.split(':')) == 8) & (IP[0] != '0'):
			hex = set('0123456789abcdefABCDEF')
			for i in IP.split(':'):
				if len(i) == 0 or len(i) > 4:
					return 'Neither'
				elif any([j not in hex for j in i]):
					return 'Neither'
			return 'IPv6'
		else:
			return 'Neither'


class Solution:

	def validIPAddress(self, IP):
		ans = 'Neither'
		if '.' in IP:
			seg = IP.split('.')
			if len(seg) == 4:
				if all((s.isdigit() and 0 <= int(s) < 256 and (not (s[0] == '0' and len(s) > 1)) for s in seg)):
					ans = 'IPv4'
		elif ':' in IP:
			seg = IP.split(':')
			if len(seg) == 8:
				if all((0 < len(s) <= 4 and all((c in string.hexdigits for c in s)) for s in seg)):
					ans = 'IPv6'
		return ans