import java.util.*;
public class Solution {
// 做起来比较费事,需要考虑到各种异常情况
// 验证输入的字符串是否是有效的IPv4或IPv6
public String solve (String IP) {
// write code here
int length = IP.length();
if(length > 15){
if(IPv6(IP)){
return "IPv6";
}
}else{
if(IPv4(IP)){
return "IPv4";
}
}
return "Neither";
}
public boolean IPv4(String IP){
String[] strs = IP.split("\\.");
if(strs.length != 4){
return false;
}
for(String str : strs){
if(str.charAt(0) == '0'){
return false;
}
try{
int num = Integer.parseInt(str);
if(num >= 255 || num < 0){
return false;
}
} catch(Exception e){
return false;
}
}
return true;
}
public boolean IPv6(String IP){
if(IP.charAt(IP.length() - 1) == ':'){
return false;
}
String[] strs = IP.split(":");
if(strs.length != 8){
return false;
}
for(String str : strs){
if(str.length() > 4 || str.length() == 0){
return false;
}
if(str.length() == 1 && !str.equals("0")){
return false;
}
try{
int max = Integer.parseInt("FFFF", 16);
int min = 0;
int num = Integer.parseInt(str, 16);
if(num >= max || num < 0){
return false;
}
}catch(Exception e){
return false;
}
}
return true;
}
}