# leetcode 468. Verify IP address

Posted by delboy1978uk on Tue, 08 Feb 2022 20:02:15 +0100

Title Description
Write a function to verify whether the input string is a valid IPv4 or IPv6 address.
If it is a valid IPv4 address, return "IPv4";
If it is a valid IPv6 address, return "IPv6";
If it is not the IP address of the above type, return "Neither".
IPv4 addresses are represented by decimal numbers and dots. Each address contains four decimal numbers, with the range of 0 - 255 and (".) division. For example, 172.16.254.1;

IPv6 addresses are represented by 8 groups of hexadecimal digits, each group representing 16 bits. These groups of numbers are divided by (":"). For example, 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid address. Moreover, we can add some numbers starting with 0. The letters can be uppercase or lowercase. Therefore, 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address (that is, ignore the beginning of 0 and ignore case).

However, we cannot use an empty group because the value of a group is 0, so that (::) occurs. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.

At the same time, redundant zeros are not allowed in IPv6 addresses. For example, 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.

Example 1:
Input: IP = "172.16.254.1"
Output: "IPv4"
Explanation: valid IPv4 address, return "IPv4"
Example 2:
Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: valid IPv6 address, return "IPv6"
Example 3:
Input: IP = "256.256.256.256"
Output: "Neither"
Example 4:
Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334:"
Output: "Neither"
Example 5:
Input: IP = "1e1.4.5.6"
Output: "Neither"
Tips:
IP consists of only English letters, numbers and characters' And ':'.

Problem solution
The hint means that you should not consider the situation that can be abbreviated in IPV6.
There are too few test samples for the corresponding topics on Niuke online, and the wrong program will be passed. So write it on LeetCode.

This topic needs to verify two IP addresses. The two IP addresses are distinguished in different ways. IPV4 is marked with '.' IPV6 is distinguished by ':',

• First, if two symbols appear in a string, this kind of string does not belong to the above two addresses

• First, you need to implement the split function in Python to "." "," is the dividing point. Add the substring in the string into the vector, and then make the following judgment. When passing in ip, add the corresponding delimiter at the end

• If only "." is found Then it should be judged according to the way of IPV4

• If the number of items in the vector is not equal to 4, it is illegal
• If the length of each item is empty or greater than 3, it is illegal
• If the length of each character is greater than 1 and the first character is 0, (leading 0) is illegal
• It is also illegal if the range of characters in each item is not 0-9
• If each item is converted to a number and greater than 255, it is also illegal
• If only ":" is found, judge according to IPV6

• If the number of items in the vector is not equal to 8, it is illegal
• If the length of each item is empty or greater than 4, it is illegal
• If the range of characters in each item is not 0-9 or A-F or A-F, it is also illegal
class Solution {
public:
vector<string> split(string ip,char t)
{
vector<string> res;
for(int i=0;i<ip.size();i++)
{
int j=i;
string items;
while(ip[j]!=t)
{
items+=ip[j];
j++;
}
i=j;
res.push_back(items);
}
return res;
}
string check_ipv4(string ip)
{
auto items=split(ip+'.','.');
if(items.size()!=4) return "Neither";
for(auto x:items)
{
if(x.empty()||x.size()>3) return "Neither";
if(x[0]=='0' && x.size()>1) return "Neither";
for(char c:x)
{
if(c<'0' ||c>'9') return "Neither";
}
int t=stoi(x);
if(t>255) return "Neither";
}
return "IPv4";

}

bool check(char c)
{
if(c>='0' &&c<='9') return true;
if(c>='a' &&c<='f') return true;
if(c>='A' &&c<='F') return true;
return false;
}

string check_ipv6(string ip)
{
auto items=split(ip+':',':');
if(items.size()!=8) return "Neither";
for(auto x:items)
{
if(x.empty()||x.size()>4) return "Neither";
for(auto c:x)
{
if(!check(c)) return "Neither";
}
}
return "IPv6";
}