[Leetcode] Validate IP address Authentication IP addresses

Source: Internet
Author: User

In this problem, your job to write a function to check whether a input string is a valid IPV4 address or IPV6 address or n either.

IPV4 addresses is canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging F Rom 0 to 255, separated by dots ("."), e.g., 172.16.254.1 ;

Besides, you need-keep in mind that leading zeros in the IPV4 is illegal. For example, the address is 172.16.254.01 illegal.

IPV6 addresses is represented as eight groups of four hexadecimal digits, each group representing bits. The groups is separated by colons (":"). For example, the address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334 a legal one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper- Case ones, 2001:db8:85a3:0:0:8A2E:0370:7334 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 the consecutive colons (::) to Pursue simplicity. For example, was an 2001:0db8:85a3::8A2E:0370:7334 invalid IPV6 address.

Besides, you need-keep in mind that extra leading zeros in the IPV6 is also illegal. For example, the address is 02001:0db8:85a3:0000:0000:8a2e:0370:7334 also illegal.

Note:you could assume there is no. extra space in the test cases and there could some special characters in the input string .

Example 1:

Input: "172.16.254.1" Output: "IPv4" Explanation:this is a valid IPV4 address, return "IPV4".

Example 2:

Input: "2001:0db8:85a3:0:0:8a2e:0370:7334" Output: "IPv6" Explanation:this is a valid IPV6 address, return "IPV6".

Example 3:

Input: "256.256.256.256" Output: "Neither" explanation:this is neither a IPv4 address nor a IPV6 address.

This problem let us verify the two IP addresses, leetcode before there is a question about IPv4 restore IP Addresses, gave us a string, let us by the middle dot to find all the correct IP address, this problem gives us a dot or a colon string, Let's see if it's the right IPv4 or IPv6, and it feels a little bit more complicated. So we only have to verify the difference, then how can we quickly distinguish between IPv4 or IPv6, of course, through the middle point or colon, so we first find a colon in the string (of course you want to find a point also can), if there is no colon in the string, then we have to verify whether it is IPV4, If there is a colon, we can verify that it is IPv6.

First for IPV4, we use the Getline function to intercept the string between two points, we also need a counter CNT to record how many segments we have intercepted, if CNT is greater than 4, indicating that more than 4 paragraphs, indicating the correct address. If the extracted string is empty, it means that the two points are connected together and not. If the string is longer than 1 and the first character is 0, it is not. Since the address of the IPV4 is between 0 and 255, it is also incorrect if the string length is greater than 3. Below we examine each character, if there is a character that is not a number, returns neither. Finally, we will convert the string to a number, if it is not between 0 to 255 is illegal. Finally, we want to ensure that CNT is exactly 4, and that the last character cannot be a point, and all of the above conditions are the correct IPV4 address.

Then for IPV6, we also use the Getline function to intercept the string between two colons, we also need the counter CNT to record how many segments we have intercepted, if CNT is greater than 8, indicating that more than 8 paragraphs, indicating is not the correct address. If the extracted string is empty, the two colons are joined together and are not correct. Face we check each character, the correct character should be a number between 0 and 9, or a to f, or a to F character, if other characters appear, return neither. Finally, we want to ensure that CNT is exactly 8, and that the last character cannot be a colon, all of which satisfy the above conditions is the correct IPV6 address.

classSolution { Public:    stringValidipaddress (stringIP) {Istringstream is(IP); stringt =""; intCNT =0; if(Ip.find (':') ==string:: NPOs) {//Check IPv4             while(Getline ( isT'.')) {                ++CNT; if(CNT >4|| T.empty () | | (T.size () >1&& t[0] =='0') || T.size () >3)return "neither";  for(Charc:t) {if(C <'0'|| C >'9')return "neither"; }                intval =Stoi (t); if(Val <0|| Val >255)return "neither"; }            return(CNT = =4&& ip.back ()! ='.') ?"IPv4":"neither"; } Else{//Check IPv6             while(Getline ( isT':')) {                ++CNT; if(CNT >8|| T.empty () | | T.size () >4)return "neither";  for(Charc:t) {if(! (c >='0'&& C <='9') &&! (c >='a'&& C <='F') &&! (c >='A'&& C <='F'))return "neither"; }            }            return(CNT = =8&& ip.back ()! =':') ?"IPv6":"neither"; }    }};

Similar topics:

Restore IP Addresses

Resources:

Https://discuss.leetcode.com/topic/71572/java-solution

Https://discuss.leetcode.com/topic/71418/short-regexp-solution/5

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Validate IP address Authentication IP addresses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.