Given a string queryIP, return "IPv4" if IP is a valid IPv4 address, "IPv6" if IP is a valid IPv6 address or "Neither" if IP is not a correct IP of any type.

A valid IPv4 address is an IP in the form "x1.x2.x3.x4" where 0 &lt;= xi &lt;= 255 and xi cannot contain leading zeros. For example, "192.168.1.1" and "192.168.1.0" are valid IPv4 addresses while "192.168.01.1", "192.168.1.00", and "192.168@1.1" are invalid IPv4 addresses.

A valid IPv6 address is an IP in the form "x1:x2:x3:x4:x5:x6:x7:x8" where:


	1 &lt;= xi.length &lt;= 4
	xi is a hexadecimal string which may contain digits, lowercase English letter ('a' to 'f') and upper-case English letters ('A' to 'F').
	Leading zeros are allowed in xi.


For example, "2001:0db8:85a3:0000:0000:8a2e:0370:7334" and "2001:db8:85a3:0:0:8A2E:0370:7334" are valid IPv6 addresses, while "2001:0db8:85a3::8A2E:037j:7334" and "02001:0db8:85a3:0000:0000:8a2e:0370:7334" are invalid IPv6 addresses.

&nbsp;
Example 1:

Input: queryIP = "172.16.254.1"
Output: "IPv4"
Explanation: This is a valid IPv4 address, return "IPv4".


Example 2:

Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: This is a valid IPv6 address, return "IPv6".


Example 3:

Input: queryIP = "256.256.256.256"
Output: "Neither"
Explanation: This is neither a IPv4 address nor a IPv6 address.


&nbsp;
Constraints:


	queryIP consists only of English letters, digits and the characters '.' and ':'.


class Solution:
    def validIPAddress(self, queryIP: str) -> str:
        queryIP = queryIP.replace(".",":")
        ct = 0
        for i in queryIP.split(":"):
            if i != "":
                ct += 1
        if ct == 4:
            for i in queryIP.split(":"):
                if i == "":
                    return "Neither"
                if i.isnumeric():
                    if len(i) > 1:
                        if i.count('0') == len(i) or int(i) > 255 or i[0] == '0':
                            return "Neither"
                else:
                    return "Neither"
            return "IPv4"
        elif ct == 8:
            a = ['a','b','c','d','e','f','A','B','C','D','E','F']
            for i in queryIP.split(":"):
                if i == "":
                    return "Neither"
                if len(i) < 5:
                    for j in i:
                        if j not in a and j.isdigit() == False:
                            return "Neither"
                else:
                    return "Neither"
            return "IPv6"
        else:
            return "Neither"
                   


        
class Solution {
    public String validIPAddress(String queryIP) {
        String regexIpv4 = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
        
        String regexIpv6 = "((([0-9a-fA-F]){1,4})\\:){7}(([0-9a-fA-F]){1,4})";
        
        if(queryIP.matches(regexIpv4))
            return "IPv4";
        else if(queryIP.matches(regexIpv6))
            return "IPv6";
        else
            return "Neither";
    }
}
class Solution {
public:
    bool checkforIPv6(string IP){
        int n = IP.size();
        vector<string>store;
        string s = "";
        for(int i=0; i<n; i++){
            if(IP[i] == ':'){
                store.push_back(s);
                s = "";
            }
            else{
                s+=IP[i];
            }
        }
        store.push_back(s);
        if(store.size() != 8){
            return false;
        }
        for(int i=0; i<store.size(); i++){
            string s = store[i];
            if(s.size() > 4 or s.size() == 0){
                return false;
            }
            for(int j=0; j<s.size(); j++){
                if(s[j] >= 'a' and s[j] <= 'f'){
                    continue;
                }
                else if(s[j] >= 'A' and s[j] <= 'F'){
                    continue;
                }
                else if(s[j] >= '0' and s[j] <= '9'){
                    continue;
                }
                else{
                    return false;
                }
            }
        }
        return true;
    }
    
    bool checkforIPv4(string IP){
        int n = IP.size();
        vector<string>store;
        string s = "";
        for(int i=0; i<n; i++){
            if(IP[i] == '.'){
                store.push_back(s);
                s = "";
            }
            else{
                s+=IP[i];
            }
        }
        store.push_back(s);
        if(store.size() != 4){
            return false;
        }
        for(int i=0; i<store.size(); i++){
            string s = store[i];
            if(s.size() > 3 or s.size() == 0){
                return false;
            }
            int num = 0;
            for(int j=0; j<s.size(); j++){
                if(s.size() >= 2 and s[0] == '0' and s[1] == '0'){
                    return false;
                }
                if(s.size() >= 2 and s[0] == '0' and s[1] != '0'){
                    return false;
                }
                if(s[j] >= '0' and s[j] <= '9'){
                    // Do nothing.
                }
                else{
                    return false;
                }
                num = num*10 + (s[j]-'0');
            }
            if(num > 255 or num < 0){
                return false;
            }
        }
        return true;
    }
    string validIPAddress(string queryIP) {
        bool IPv6 = checkforIPv6(queryIP);
        bool IPv4 = checkforIPv4(queryIP);
        if(IPv6){
            return "IPv6";
        }
        if(IPv4){
            return "IPv4";
        }
        return "Neither";
    }
};
var validIPAddress = function(queryIP) {
    const iPv4 = () => {
        const address = queryIP.split('.');
        if (address.length !== 4) return null;

        for (const str of address) {
            const ip = parseInt(str);
            if (ip < 0 || ip > 255) return null;
            if (ip.toString() !== str) return null;
        }
        return 'IPv4';
    };

    const iPv6 = () => {
        const address = queryIP.split(':');
        if (address.length !== 8) return null;
        const config = '0123456789abcdefABCDEF';
        const check = address.every(str => {
            if (str === '' || str.length > 4) return false;
            for (const s of str) {
                if (!config.includes(s)) return false;
            }
            return true;
        });
        return check ? 'IPv6' : null;
    };

    return iPv4() ?? iPv6() ?? 'Neither';
};
Validate IP Address