forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
29 lines (27 loc) · 904 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def validIPAddress(self, queryIP: str) -> str:
def is_ipv4(s: str) -> bool:
ss = s.split(".")
if len(ss) != 4:
return False
for t in ss:
if len(t) > 1 and t[0] == "0":
return False
if not t.isdigit() or not 0 <= int(t) <= 255:
return False
return True
def is_ipv6(s: str) -> bool:
ss = s.split(":")
if len(ss) != 8:
return False
for t in ss:
if not 1 <= len(t) <= 4:
return False
if not all(c in "0123456789abcdefABCDEF" for c in t):
return False
return True
if is_ipv4(queryIP):
return "IPv4"
if is_ipv6(queryIP):
return "IPv6"
return "Neither"