468. Validate IP Address


http://bookshadow.com/weblog/2016/12/11/leetcode-validate-ip-address/


The hexadecimal numbers are 0-9 and then use the letters A-F.





May I ask why we use split("\\.") rather than split("\.") here? Because I think we split the string with "." here. Thanks.


a single dot means any character in RegExp, \\. means dot



dot ('.') in regex means "any character", so you need a backslash to escape it. And double backslash needed to create a single backslash in the regex.

// passed 75/79 cases



class Solution {
public String validIPAddress(String IP) {
if(isIPv4(IP)) return "IPv4";
else if(isIPv6(IP)) return "IPv6";
else return "Neither";
}
private boolean isIPv4(String IP){
// 4 parts, three dots
if(countSplit(IP, '.') != 3) return false;
String[] tokens = IP.split("\\."); ///////
if(tokens.length != 4) return false;
for(String token : tokens){
if(!isIPv4Token(token)) return false;
}
return true;

}
private boolean isIPv4Token(String token){
// check length first
if(token.length() == 0 || token.length() > 3) return false;
// check leading 0 , single 0 is fine, not 02, 011 etc
if(token.charAt(0) == '0' && token.length() > 1) return false;
// if(token.startWith("0") && token.length() > 1) return false;
// this token must have only numbers
if(!isNumeric(token)) return false;
// check token value is between 0 and 255
int val = Integer.valueOf(token);
if(val < 0 || val > 255) return false;
return true;
}
private boolean isIPv6(String IP){
// 8 parts, seven :
if(countSplit(IP, ':') != 7) return false;
String[] tokens = IP.split(":");
if(tokens.length != 8) return false;
for(String token : tokens){
if(!isIPv6Token(token)) return false;
}
return true;
}
private boolean isIPv6Token(String token){
// check token length
// || token.length() > 4
if(token.length() == 0 || token.length() > 4) return false;
// leading 0 is okay , single 0 is okay, no need to check 0
// check char in every token, can include only letters and numbers
for(char c : token.toCharArray()){
if(!Character.isLetterOrDigit(c)) return false;
// if((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') for checking letter
}
return true;
}
private int countSplit(String IP, char split){
int count = 0;
for(char ch : IP.toCharArray()){
if(ch == split){
count++;
}
}
return count;
}
private boolean isNumeric(String token){
for(char c : token.toCharArray()){
if(!Character.isDigit(c)) return false;
}
return true;
}
}


 




Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.

IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,​​172.16.254.1​​;

Besides, leading zeros in the IPv4 is invalid. For example, the address ​​172.16.254.01​​ is invalid.

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

Besides, extra leading zeros in the IPv6 is also invalid. For example, the address ​​02001:0db8:85a3:0000:0000:8a2e:0370:7334​​ is invalid.

Note: You may assume there is no extra space or 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.