-
Notifications
You must be signed in to change notification settings - Fork 0
/
468.validate-ip-address.java
60 lines (50 loc) · 1.77 KB
/
468.validate-ip-address.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public String validIPAddress(String IP) {
if(IP.length() == 0 )
return "Neither";
String[] digits = IP.split("\\.", 0);
if(IP.charAt(IP.length()-1) == '.' || IP.charAt(IP.length()-1) == ':')
return "Neither";
if(!digits[0].equals(IP)){
if(digits.length != 4)
return "Neither";
for(String s: digits){
try{
if(s.charAt(0) == '0' && s.length() <=3 && s.length() !=1 || Integer.parseInt(s) > 255)
return "Neither";
}
catch(Exception e){
return "Neither";
}
for(char c : s.toCharArray()){
if(Character.getNumericValue(c)<0 || Character.getNumericValue(c)>9)
return "Neither";
}
}
return "IPv4";
}
else{
digits = IP.split(":", 0);
if(digits.length != 8)
return "Neither";
for(String s: digits){
if(s.length()>4 || s.length()==0)
return "Neither";
for(char c : s.toCharArray()){
if(c == '-'){
System.out.println(c);
return "Neither";
}
if(Character.isDigit(c)) {
continue;
}
if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
return "Neither";
}
}
}
return "IPv6";
}
// return "Neither4";
}
}