bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 468. Validate IP Address

468. Validate IP Address

Leetcode Solutions

Divide and Conquer Approach

  1. Check if the input string contains a '.'. If it does, attempt to validate it as an IPv4 address.
  2. Split the string by '.' and check if there are exactly four chunks.
  3. For each chunk, check if it's a valid decimal number between 0 and 255 without leading zeros.
  4. If all chunks are valid, return 'IPv4'.
  5. If the input string contains a ':', attempt to validate it as an IPv6 address.
  6. Split the string by ':' and check if there are exactly eight chunks.
  7. For each chunk, check if it's a valid hexadecimal string of length 1 to 4.
  8. If all chunks are valid, return 'IPv6'.
  9. If neither condition is met, return 'Neither'.
UML Thumbnail

Regular Expression (Regex) Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...