Similar Problems
Similar Problems not available
Validate Ip Address - Leetcode Solution
LeetCode: Validate Ip Address Leetcode Solution
Difficulty: Medium
Topics: string
Problem Statement:
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.
Examples:
Input: "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4".
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6".
Input: "256.256.256.256" Output: "Neither" Explanation: This is neither a IPv4 address nor a IPv6 address.
Approach:
The problem statement clearly states the format of valid IPv4 and IPv6 addresses. The solution involves splitting the input string based on the separator (dot "." for IPv4 and colon ":" for IPv6) and validating each group at each index.
For IPv4: Each group should be in the range of 0 to 255 (inclusive) and should not contain any leading zeros.
For IPv6: Each group should be a hexadecimal number of exactly 4 digits. The input string can have lower-case alphabets, which can be converted to upper-case alphabets. Also, consecutive groups of zero can only be replaced by "::" if there are no other zero groups in-between them. So the total number of groups in the final string cannot exceed 8.
Solution:
- First, we split the input string based on the separator (dot "." or colon ":").
- If the string has neither 3 dots nor 7 colons, return "Neither"
- If the string has 3 dots but not 4 groups after splitting, return "Neither"
- If the string has 7 colons but not 8 groups after splitting, return "Neither"
- If the string has both dots and colons, return "Neither" (it can't be both IPv4 and IPv6)
- For IPv4, check if each group is a decimal number between 0 and 255 (inclusive) and doesn't have leading zeros.
- For IPv6, check if each group is a hexadecimal number of exactly 4 digits and the total number of groups in the final string doesn't exceed 8.
Code:
Here is the commented Python code for the above approach:
def validateIPAddress(IP: str) -> str:
# Check if IPv4 - if yes, validate and return "IPv4"
if '.' in IP:
groups = IP.split('.')
if len(groups) != 4: # should have 4 groups
return "Neither"
for group in groups:
if not group.isdigit() or not 0 <= int(group) <= 255 or (group[0] == '0' and len(group) > 1):
# should contain only digits, be between 0 and 255, and not have leading zeros
return "Neither"
return "IPv4"
# Check if IPv6 - if yes, validate and return "IPv6"
elif ':' in IP:
groups = IP.split(':')
if len(groups) != 8: # should have 8 groups
return "Neither"
for group in groups:
# should contain only hexadecimal digits (0-9, a-f or A-F) and have exactly 4 digits
if not all(char in '0123456789abcdefABCDEF' for char in group) or len(group) > 4 or group.count('0') > 3:
return "Neither"
# Replace consecutive zero groups with "::" and count total number of groups
IP = IP.replace('::', ':'+':'*8, 1)
if IP.count(':') != 7: # should have exactly 8 groups after replacement
return "Neither"
return "IPv6"
# Neither IPv4 nor IPv6
return "Neither"
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(1). We only store the groups after splitting the input string.
Validate Ip Address Solution Code
1