<p>Given a string <code>queryIP</code>, return <code>&quot;IPv4&quot;</code> if IP is a valid IPv4 address, <code>&quot;IPv6&quot;</code> if IP is a valid IPv6 address or <code>&quot;Neither&quot;</code> if IP is not a correct IP of any type.</p>

<p><strong>A valid IPv4</strong> address is an IP in the form <code>&quot;x<sub>1</sub>.x<sub>2</sub>.x<sub>3</sub>.x<sub>4</sub>&quot;</code> where <code>0 &lt;= x<sub>i</sub> &lt;= 255</code> and <code>x<sub>i</sub></code> <strong>cannot contain</strong> leading zeros. For example, <code>&quot;192.168.1.1&quot;</code> and <code>&quot;192.168.1.0&quot;</code> are valid IPv4 addresses while <code>&quot;192.168.01.1&quot;</code>, <code>&quot;192.168.1.00&quot;</code>, and <code>&quot;192.168@1.1&quot;</code> are invalid IPv4 addresses.</p>

<p><strong>A valid IPv6</strong> address is an IP in the form <code>&quot;x<sub>1</sub>:x<sub>2</sub>:x<sub>3</sub>:x<sub>4</sub>:x<sub>5</sub>:x<sub>6</sub>:x<sub>7</sub>:x<sub>8</sub>&quot;</code> where:</p>

<ul>
	<li><code>1 &lt;= x<sub>i</sub>.length &lt;= 4</code></li>
	<li><code>x<sub>i</sub></code> is a <strong>hexadecimal string</strong> which may contain digits, lowercase English letter (<code>&#39;a&#39;</code> to <code>&#39;f&#39;</code>) and upper-case English letters (<code>&#39;A&#39;</code> to <code>&#39;F&#39;</code>).</li>
	<li>Leading zeros are allowed in <code>x<sub>i</sub></code>.</li>
</ul>

<p>For example, &quot;<code>2001:0db8:85a3:0000:0000:8a2e:0370:7334&quot;</code> and &quot;<code>2001:db8:85a3:0:0:8A2E:0370:7334&quot;</code> are valid IPv6 addresses, while &quot;<code>2001:0db8:85a3::8A2E:037j:7334&quot;</code> and &quot;<code>02001:0db8:85a3:0000:0000:8a2e:0370:7334&quot;</code> are invalid IPv6 addresses.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> queryIP = &quot;172.16.254.1&quot;
<strong>Output:</strong> &quot;IPv4&quot;
<strong>Explanation:</strong> This is a valid IPv4 address, return &quot;IPv4&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> queryIP = &quot;2001:0db8:85a3:0:0:8A2E:0370:7334&quot;
<strong>Output:</strong> &quot;IPv6&quot;
<strong>Explanation:</strong> This is a valid IPv6 address, return &quot;IPv6&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> queryIP = &quot;256.256.256.256&quot;
<strong>Output:</strong> &quot;Neither&quot;
<strong>Explanation:</strong> This is neither a IPv4 address nor a IPv6 address.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>queryIP</code> consists only of English letters, digits and the characters <code>&#39;.&#39;</code> and <code>&#39;:&#39;</code>.</li>
</ul>

class Solution {
    bool ipv4 (string a) {
        bool h;
        h = true;

        int w;
        w = 0;

        int l;
        for (l=0;l<=(a.size () -1);l++) {
            if (a[l] == '.') {
                w++;
            }
        }

        if (w != 3) {
            return false;
        }

        int count;
        count = 0;

        vector<string> arr;

        string b;

        int i;
        for (i=0;i<=(a.size () -1);i++) {
            if (a[i] == '.') {
                count++;
                if (b.size () == 0) {
                    return false;
                }

                else {
                    arr.push_back (b);
                    int d;
                    d = stoi (b);

                    if (d >= 256) {
                        return false;
                    }

                    b = "";
                }
            }

            else {
                if (a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <= 'Z') {
                    return false;
                }

                else {
                    if (b.size () > 3) {
                        return false;
                    }

                    b = b + a[i];
                }
            }
        }

        if (count != 3) {
            return false;
        }

        if (b.size () >= 1) {
            int e;
            e = stoi (b);
            if (e >= 256) {
                return false;
            }

            arr.push_back (b);
        }

        if (arr.size () != 4) {
            return false;
        }

        int j;
        int k;
        for (j=0;j<=(arr.size () -1);j++) {
            string g;
            g = arr[j];

            if (g[0] == '0' && g.size () > 1) {
                return false;
            }
        }

        return h;
    }

    bool ipv6 (string q) {
        bool r;
        r = true;

        int w;
        w = 0;

        int l;
        for (l=0;l<=(q.size () -1);l++) {
            if (q[l] == ':') {
                w++;
            }
        }

        if (w != 7) {
            return false;
        }

        vector<string> arr;

        string a;

        int count;
        count = 0;

        int i;
        for (i=0;i<=(q.size () -1);i++) {
            if (q[i] == ':') {
                count++;
                if (a.size () == 0) {
                    return false;
                }

                if (a.size () > 4) {
                    return false;
                }

                arr.push_back (a);
                a = "";
            }

            else {
                if (q[i] >= 'g' && q[i] <= 'z' || q[i] >= 'G' && q[i] <= 'Z') {
                    return false;
                }

                else {
                    if (a.size () > 4) {
                        return false;
                    }
                    
                    a = a + q[i];
                }
            }
        }

        if (a.size () == 0 || a.size () > 4) {
            return false;
        }

        arr.push_back (a);

        if (count != 7) {
            return false;
        }

        if (arr.size () != 8) {
            return false;
        }

        return r;
    }
public:
    string validIPAddress(string queryIP) {
        string ans;

        string t;
        t = "Neither";

        if (queryIP.size () == 0) {
            return t;
        }

        if (ipv4 (queryIP) == true) {
            return "IPv4";
        }

        else if (ipv6 (queryIP) == true) {
            return "IPv6";
        }

        ans = t;

        return ans;
    }
};
8000