Advertisement

Validate IP Address - LeetCode 468 Solution

Validate IP Address - Complete Solution Guide

Validate IP Address is LeetCode problem 468, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 "x 1 .x 2 .x 3 .x 4 " where 0 <= x i <= 255 and x i 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 "[email protected]" are invalid IPv4 addresses. A valid IPv6 address is an IP in the form "x 1 :x 2 :x 3 :x 4

Detailed Explanation

The problem requires validating if a given string `queryIP` is a valid IPv4 address, a valid IPv6 address, or neither. A valid IPv4 address consists of four decimal numbers (0-255) separated by dots ('.'), with no leading zeros. A valid IPv6 address consists of eight hexadecimal numbers (0-FFFF) separated by colons (':'). Leading zeros are allowed in IPv6 parts. The function should return 'IPv4', 'IPv6', or 'Neither' based on the input string.

Solution Approach

The solution approach involves three main steps: 1. Determine the IP version (IPv4 or IPv6) by counting the occurrences of '.' and ':' characters. 2. Split the input string based on the delimiter ('.' for IPv4, ':' for IPv6). 3. Validate each part based on the rules of the corresponding IP version. If any validation fails, return 'Neither'. If all validations pass, return the IP version ('IPv4' or 'IPv6').

Step-by-Step Algorithm

  1. Step 1: Count the number of '.' and ':' characters in the input `queryIP`.
  2. Step 2: If the count of '.' is 3, assume it's potentially an IPv4 address and call `validate_ipv4()`.
  3. Step 3: If the count of ':' is 7, assume it's potentially an IPv6 address and call `validate_ipv6()`.
  4. Step 4: If neither condition is met, return 'Neither'.
  5. Step 5: In `validate_ipv4()`, split the string by '.' delimiter.
  6. Step 6: Iterate through each part of the IPv4 address:
  7. Step 7: Check if the part contains only digits. If not, return 'Neither'.
  8. Step 8: Check for leading zeros in parts with length greater than 1. If present, return 'Neither'.
  9. Step 9: Convert the part to an integer and check if it's between 0 and 255 (inclusive). If not, return 'Neither'.
  10. Step 10: If all parts are valid, return 'IPv4'.
  11. Step 11: In `validate_ipv6()`, split the string by ':' delimiter.
  12. Step 12: Iterate through each part of the IPv6 address:
  13. Step 13: Check if the part length is between 1 and 4 (inclusive). If not, return 'Neither'.
  14. Step 14: Attempt to convert the part to an integer with base 16 (hexadecimal). Use a try-except block to handle `ValueError` if the conversion fails. If conversion fails, return 'Neither'.
  15. Step 15: If all parts are valid, return 'IPv6'.

Key Insights

  • Insight 1: The problem can be solved by first identifying whether the input is potentially IPv4 or IPv6 based on the presence of '.' or ':' characters.
  • Insight 2: String splitting is essential to extract the individual components of the IP address.
  • Insight 3: Validation of each component requires checking for numeric or hexadecimal format, valid range (0-255 for IPv4, 0-FFFF for IPv6), and the absence of leading zeros in IPv4.
  • Insight 4: Error handling, especially using try-except blocks, is crucial for handling invalid input formats during number conversions.
  • Insight 5: The number of parts after splitting the IP address should exactly match the IP version. Four for IPv4, eight for IPv6.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: String.

Companies

Asked at: Cisco, Deutsche Bank, Flexport, Nvidia, ServiceNow, Sprinklr, Turing, X.