package leetcode;

/**
 * https://leetcode.com/problems/validate-ip-address/
 */
public class Problem468 {
    public String validIPAddress(String IP) {
        if (IP.contains(".")) {
            if (isValidIPv4(IP)) {
                return "IPv4";
            }
            return "Neither";
        }
        if (isValidIPv6(IP)) {
            return "IPv6";
        }
        return "Neither";
    }

    private static boolean isValidIPv6(String ip) {
        // TODO
        return false;
    }

    private static boolean isValidIPv4(String ip) {
        String[] parts = ip.split("\\.");
        if (parts.length != 4) {
            return false;
        }
        for (String part : parts) {
            int number = Integer.parseInt(part);
            if (0 <= number && number <= 255) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Problem468 prob = new Problem468();
        System.out.println(prob.validIPAddress("172.16.254.1")); // IPv4
        System.out.println(prob.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334")); // IPv6
        System.out.println(prob.validIPAddress("256.256.256.256")); // Neither
    }
}

@@ -158,14 +158,52 @@
     
-if (IP
+String ip = IP.toLowerCase();%0A        if (ip
 .con
@@ -244,18 +244,18 @@
 lidIPv4(
-IP
+ip
 )) %7B%0A   
@@ -360,18 +360,18 @@
 lidIPv6(
-IP
+ip
 )) %7B%0A   
@@ -501,16 +501,605 @@
     
-// TODO%0A
+String%5B%5D parts = ip.split(%22:%22, -1);%0A        if (parts.length != 8) %7B%0A            return false;%0A        %7D%0A        for (String part : parts) %7B%0A            if (part.length() %3E 4) %7B%0A                return false;%0A            %7D%0A            if (!isHex(part)) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A%0A    private static boolean isHex(String str) %7B%0A        if (str.equals(%22+0%22) %7C%7C str.equals(%22-0%22)) %7B%0A            return false;%0A        %7D%0A        try %7B%0A            Integer.parseInt(str, 16);%0A            return true;%0A        %7D catch (NumberFormatException e) %7B%0A    
     
@@ -1108,32 +1108,42 @@
   return false;%0A
+        %7D%0A
     %7D%0A%0A    priva
@@ -1224,16 +1224,20 @@
 it(%22%5C%5C.%22
+, -1
 );%0A     
@@ -1328,32 +1328,216 @@
 part : parts) %7B%0A
+            if (part.startsWith(%220%22) && part.length() %3E 1) %7B%0A                return false;%0A            %7D%0A            if (!isNumber(part)) %7B%0A                return false;%0A            %7D%0A
             int 
@@ -1589,30 +1589,29 @@
 if (
-0 %3C= 
 number 
-&&
+%3C 0 %7C%7C
  number 
 %3C= 2
@@ -1610,10 +1610,9 @@
 ber 
-%3C=
+%3E
  255
@@ -1630,35 +1630,36 @@
          return 
-tru
+fals
 e;%0A            %7D
@@ -1680,28 +1680,27 @@
      return 
-fals
+tru
 e;%0A    %7D%0A%0A  
@@ -1706,21 +1706,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -1720,324 +1720,280 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem468 prob = new Problem468();%0A        System.out.println(prob.validIPAddress(%22172.16.254.1%22)); // IPv4%0A        System.out.println(prob.validIPAddress(%222001:0db8:85a3:0:0:8A2E:0370:7334%22)); // IPv6%0A        System.out.println(prob.validIPAddress(%22256.256.256.256%22)); // Neither
+boolean isNumber(String str) %7B%0A        if (str.equals(%22+0%22) %7C%7C str.equals(%22-0%22)) %7B%0A            return false;%0A        %7D%0A        try %7B%0A            Integer.parseInt(str);%0A            return true;%0A        %7D catch (NumberFormatException e) %7B%0A            return false;%0A        %7D
 %0A   

Solve problem 468