import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
/**
*
* You must write a method in such a way that it can be used to validate an IP address.
* Use the following definition of an IP address:
IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range
from 0 to 255. Leading zeros are allowed.
Some valid IP address:
000.12.12.034
121.234.12.12
23.45.12.56
Some invalid IP address:
000.12.234.23.23
666.666.23.23
.213.123.23.32
23.45.22.32.
I.Am.not.an.ip
Sample Input
000.12.12.034
121.234.12.12
23.45.12.56
00.12.123.123123.123
122.23
Hello.IP
Sample Output
true
true
true
false
false
false
*
*
*
* */
class Solution{
public static boolean isValidIP
(String ip
) { if (ip == null || ip.isEmpty()) {
return false;
}
String[] parts
= ip.
split("\\."); if (parts.length != 4) {
return false;
}
if (!isValidSegment(part)) {
return false;
}
}
return true;
}
private static boolean isValidSegment
(String segment
) { try {
int num
= Integer.
parseInt(segment
); if (num < 0 || num > 255) {
return false;
}
if (segment.length() > 1 && segment.charAt(0) == '0') {
return true;
}
return true;
return false;
}
}
public static void main
(String[] args
){ Scanner in
= new Scanner
(System.
in); while(in.hasNext()){
System.
out.
println(isValidIP
(IP
)); System.
out.
println("IP->"+IP
); }
}
}
aW1wb3J0IGphdmEudXRpbC5yZWdleC5NYXRjaGVyOwppbXBvcnQgamF2YS51dGlsLnJlZ2V4LlBhdHRlcm47CmltcG9ydCBqYXZhLnV0aWwuU2Nhbm5lcjsKCgovKioKICogCiAqIFlvdSBtdXN0IHdyaXRlIGEgbWV0aG9kIGluIHN1Y2ggYSB3YXkgdGhhdCBpdCBjYW4gYmUgdXNlZCB0byB2YWxpZGF0ZSBhbiBJUCBhZGRyZXNzLgogKiBVc2UgdGhlIGZvbGxvd2luZyBkZWZpbml0aW9uIG9mIGFuIElQIGFkZHJlc3M6CgpJUCBhZGRyZXNzIGlzIGEgc3RyaW5nIGluIHRoZSBmb3JtICJBLkIuQy5EIiwgd2hlcmUgdGhlIHZhbHVlIG9mIEEsIEIsIEMsIGFuZCBEIG1heSBy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