import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
final static String strIP6 = "0123456789ABCDEFabcdef";
final static String strIp4 = "0123456789";
// IPv4, IPv6, Neither
static public String solve(String IP) {
boolean b = checkIsIpv4(IP);
if (b) {
return "IPv4";
}
boolean c = checkIsIpv6(IP);
if (c) {
return "IPv6";
}
return "Neither";
}
public static boolean checkIsIpv6(String IP) {
if(IP.endsWith(":")){
return false;
}
String[] split = IP.split(":");
if (split.length != 8) {
return false;
}
for (String str : split) {
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
//校验字符
if (strIP6.indexOf(charArray[i]) == -1) {
return false;
}
}
if (str.isEmpty()) {
return false;
}
if (str.length() > 4) {
return false;
}
if (str.startsWith("00")) {
return false;
}
}
return true;
}
//校验是否是IPv4
public static boolean checkIsIpv4(String IP) {
String[] split = IP.split("\\.");
if (split.length != 4) {
return false;
}
for (String str : split) {
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
//校验字符
if (strIp4.indexOf(charArray[i]) == -1) {
return false;
}
}
if (str.isEmpty()) {
return false;
}
if (str.startsWith("0") && str.length() > 1) {
return false;
}
int num = Integer.parseInt(str);
if (num > 255) {
return false;
}
}
return true;
}
}