Solving Leetcode Interviews in Seconds with AI: Validate IP Address
Introduction
In this blog post, we will explore how to solve the LeetCode problem "468" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
Given a string queryIP, return "IPv4" if IP is a valid IPv4 address, "IPv6" if IP is a valid IPv6 address or "Neither" if IP is not a correct IP of any type. A valid IPv4 address is an IP in the form "x1.x2.x3.x4" where 0 <= xi <= 255 and xi cannot contain leading zeros. For example, "192.168.1.1" and "192.168.1.0" are valid IPv4 addresses while "192.168.01.1", "192.168.1.00", and "192.168@1.1" are invalid IPv4 addresses. A valid IPv6 address is an IP in the form "x1:x2:x3:x4:x5:x6:x7:x8" where: 1 <= xi.length <= 4 xi is a hexadecimal string which may contain digits, lowercase English letter ('a' to 'f') and upper-case English letters ('A' to 'F'). Leading zeros are allowed in xi. For example, "2001:0db8:85a3:0000:0000:8a2e:0370:7334" and "2001:db8:85a3:0:0:8A2E:0370:7334" are valid IPv6 addresses, while "2001:0db8:85a3::8A2E:037j:7334" and "02001:0db8:85a3:0000:0000:8a2e:0370:7334" are invalid IPv6 addresses. Example 1: Input: queryIP = "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4". Example 2: Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6". Example 3: Input: queryIP = "256.256.256.256" Output: "Neither" Explanation: This is neither a IPv4 address nor a IPv6 address. Constraints: queryIP consists only of English letters, digits and the characters '.' and ':'.
Explanation
- The approach is to first check if the input string is IPv4 or IPv6 based on the presence of '.' or ':' respectively. If neither is present, return "Neither".
- If IPv4, split the string by '.' and validate each part. If IPv6, split by ':' and validate each part.
- Return "IPv4", "IPv6", or "Neither" based on the validation result.
- Runtime Complexity: O(N), where N is the length of the input string. Storage Complexity: O(1) - excluding the space used to store the input string.
Code
def validIPAddress(queryIP: str) -> str:
"""
Given a string queryIP, return "IPv4" if IP is a valid IPv4 address,
"IPv6" if IP is a valid IPv6 address or "Neither" if IP is not a correct IP of any type.
"""
def is_ipv4(ip: str) -> bool:
parts = ip.split('.')
if len(parts) != 4:
return False
for part in parts:
if not part.isdigit():
return False
if part.startswith('0') and len(part) > 1:
return False
try:
num = int(part)
if num < 0 or num > 255:
return False
except ValueError:
return False
return True
def is_ipv6(ip: str) -> bool:
parts = ip.split(':')
if len(parts) != 8:
return False
for part in parts:
if not (1 <= len(part) <= 4):
return False
try:
int(part, 16)
except ValueError:
return False
for char in part:
if not ('0' <= char <= '9' or 'a' <= char <= 'f' or 'A' <= char <= 'F'):
return False
return True
if '.' in queryIP:
if is_ipv4(queryIP):
return "IPv4"
else:
return "Neither"
elif ':' in queryIP:
if is_ipv6(queryIP):
return "IPv6"
else:
return "Neither"
else:
return "Neither"