VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-validate-an-ip-address/

⇱ Program to validate an IP address - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to validate an IP address

Last Updated : 15 Sep, 2025

Find whether a given IP address is valid.  An IP address is a unique identifier for devices on a network, enabling internet communication. It has two versions: IPv4 and IPv6. We will validate IPv4 and IPv6 addresses separately.

IPv4 Addresses Validation

IPv4 addresses use dot-decimal notation, consisting of four numbers (0-255) separated by dots, e.g., 172.16.254.1.

Example

Input: s = "128.0.0.1";
Output: true
Explantaion: Each section split by '.' contains only digits, has no leading zeros, and lies within the range 0-255.

Input: s = "125.16.100.1";
Output: true
Explanation: Each section split by '.' contains only digits, has no leading zeros, and lies within the range 0-255.

Input: s = "125.512.100.1";
Output: false
Explanation: Each section must be within the range 0-255, but 512 exceeds this limit, making the IP invalid.

Input: s = "125.512.100.abc"
Output: false
Explanation: Each section must contain only numeric values, but "abc" is not a valid integer, making the IP invalid.

[Naive Approach] Using the Inbulit Library Methods - O(n) Time and O(n) Space

We use built-in library methods like split() in Python and Java, and stringstream in C++ to split the string by .. Then, we check if each segment lies within the range 0-255. If all segments satisfy this condition, it is a valid IPv4 address; otherwise, it is not.


Output
true

[Expected Approach] Using Traversal over the IP String - O(n) Time and O(1) Space

Iterate through the given string, extracting each section separated by .. Check if each section consists only of numeric characters and falls within the range 0-255. If any section fails these conditions, return "false", otherwise, return "true."


Output
true

IPv6 Addresses Validation

IPv6 addresses use hexadecimal colon notation, with eight groups of four hex digits (0-9, A-F) separated by colons, e.g., 2001:db8:85a3::8a2e:370:7334.

Examples

Input: s = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
Output: true
Explanation: Each section split by : contains only hexadecimal digits (0-9, a-f, A-F) and is within the valid length of 1 to 4 characters.

Input: s = "FE80::1";
Output: false
Explanation: The IPv6 address not have 8 section.

Input: s = "2001:85a3::8a2e:0370:733400";
Output: false
Explanation: Each section must be between 1 to 4 hexadecimal characters, but "733400" exceeds this limit, making the IP invalid.

Input: s = "2001:GHI8:85a3:0000:0000:8a2e:0370:7334";
Output: false
Explanation: Each section must contain only valid hexadecimal characters (0-9, a-f, A-F), but "GHI8" includes invalid characters, making the IP invalid.

[Naive Approach] Using the Inbulit Library Methods - O(n) Time and O(n) Space

The approach splits the IPv6 address into segments using built-in functions (e.g., split in Python/Java, stringstream in C++). Each segment is checked to ensure it contains only valid hexadecimal characters (0-9, a-f, A-F) and is between 1 to 4 characters long. If the total segments are not exactly 8, or any segment is invalid, the IPv6 address is considered invalid.


Output
true

[Expected Approach] Using Traversal over the IP string - O(n) Time and O(1) Space

Iterate through the given string, split the string by ':', ensuring it has 8 sections. Verify each section contains only hexadecimal characters (0-9, a-f, A-F) and is 1-4 characters long. Return "true" if all conditions are met; otherwise, return 'false'.


Output
true
Comment