Algorithm -- LeetCode 468 Verify IP address

Posted by Shuriken1 on Mon, 24 Jan 2022 20:10:41 +0100

1. Title

Original question link

Write a function to verify whether the input string is a valid IPv4 or IPv6 address.

If it is a valid IPv4 address, return "IPv4";
If it is a valid IPv6 address, return "IPv6";
"Neither" is returned if the IP address is not of the above type.

IPv4 addresses are represented by decimal numbers and dots. Each address contains four decimal numbers, with the range of 0 - 255 and (".) division. For example, 172.16.254.1;
At the same time, the number in the IPv4 address does not start with 0. For example, the address 172.16.254.01 is illegal.
IPv6 addresses are represented by 8 groups of hexadecimal digits, each group representing 16 bits. These groups of numbers are divided by (":"). For example, 2001:0db8:85a3:0000:0
000:8a2e:0370:7334 is a valid address. Moreover, we can add some numbers starting with 0. The letters can be uppercase or lowercase. So, 2001:db8:85
a3:0:0:8A2E:0370:7334 is also a valid IPv6 address (that is, ignore the beginning of 0 and ignore case).
However, we cannot use an empty group because the value of a group is 0, so that (: 😃 The situation. For example, 2001:0db8:85a3::8A2E:0370:7334
Is an invalid IPv6 address.
At the same time, redundant zeros are not allowed in IPv6 addresses. For example, 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid
.

Example 1:
Input: IP = "172.16.254.1"
Output: "IPv4"
Explanation: valid IPv4 address, return "IPv4"

Example 2:
Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: valid IPv6 address, return "IPv6"

Example 3:
Input: IP = "256.256.256.256"
Output: "Neither"
Explanation: neither IPv4 address nor IPv6 address

Example 4:
Input: IP = "2001:0db8:85a3:0:0:8A2E:0370:7334:"
Output: "Neither"

2. Problem solving

2.1 solution 1: violent solution

First split the string, and then judge whether it is IPv4 and IPv6 in turn
For IPv4 addresses, use the delimiter Divide the address into four blocks; For IPv6 address, the address is divided into eight blocks by delimiter.
For each block of IPv4 address, check whether they are within 0 - 255 and there is no leading zero.
For each block of IPv6 address, check whether its length is a hexadecimal number of 1 - 4 bits.
Note:
1. It is necessary to judge whether there is an ending Or:
2.split("\.") Need to add\

    class Solution {
        public String validIPAddress(String IP) {
            if (isIpv4(IP)) {
                return "IPv4";
            }
            if (isIpv6(IP)) {
                return "IPv6";
            }
            return "Neither";
        }

        public boolean isIpv6(String ip) {
            if (ip.indexOf('.') != -1) return false;
            // Note: if there is: or The length after segmentation is not affected, so you need to judge the end first
            if (ip.endsWith(".") || ip.endsWith(":")) return false;
            String[] strs = ip.split(":");
            if (strs.length != 8) return false;
            for (String s : strs) {
                if (s.length() == 0 || s.length() > 4) return false;
                for (char c : s.toCharArray()) {
                    if ((Character.isUpperCase(c) && c > 'F') || (Character.isLowerCase(c) && c > 'f')) {
                        return false;
                    }
                }
            }
            return true;
        }

        public boolean isIpv4(String ip) {
            // If: is included, false is returned directly
            if (ip.indexOf(':') != -1) return false;
            // Note: if there is: or The length after segmentation is not affected, so you need to judge the end first
            if (ip.endsWith(".") || ip.endsWith(":")) return false;

            String[] strs = ip.split("\\.");
            if (strs.length != 4) {
                return false;
            }
            for (String s : strs) {
                // 1. The string length is between 1-3
                if (s.length() == 0 || s.length() > 3) return false;
                // 2. There are no redundant leading zeros
                if (s.charAt(0) == '0' && s.length() != 1) return false;
                // 3. Only numbers
                for (char ch : s.toCharArray()) {
                    if (!Character.isDigit(ch)) return false;
                }
                // 4. Less than 255
                if (Integer.parseInt(s) > 255) return false;
            }

            return true;
        }
    }

reference resources:
Official explanation

Topics: Java Algorithm leetcode string