3451. Find Invalid IP Addresses
Description
Table:  logs
+-------------+---------+ | Column Name | Type | +-------------+---------+ | log_id | int | | ip | varchar | | status_code | int | +-------------+---------+ log_id is the unique key for this table. Each row contains server access log information including IP address and HTTP status code.
Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:
- Contains numbers greater than 
255in any octet - Has leading zeros in any octet (like 
01.02.03.04) - Has less or more than 
4octets 
Return the result table ordered by invalid_count, ip in descending order respectively. 
The result format is in the following example.
Example:
Input:
logs table:
+--------+---------------+-------------+ | log_id | ip | status_code | +--------+---------------+-------------+ | 1 | 192.168.1.1 | 200 | | 2 | 256.1.2.3 | 404 | | 3 | 192.168.001.1 | 200 | | 4 | 192.168.1.1 | 200 | | 5 | 192.168.1 | 500 | | 6 | 256.1.2.3 | 404 | | 7 | 192.168.001.1 | 200 | +--------+---------------+-------------+
Output:
+---------------+--------------+ | ip | invalid_count| +---------------+--------------+ | 256.1.2.3 | 2 | | 192.168.001.1 | 2 | | 192.168.1 | 1 | +---------------+--------------+
Explanation:
- 256.1.2.3 is invalid because 256 > 255
 - 192.168.001.1 is invalid because of leading zeros
 - 192.168.1 is invalid because it has only 3 octets
 
The output table is ordered by invalid_count, ip in descending order respectively.
Solutions
Solution 1: Simulation
We can determine if an IP address is invalid based on the following conditions:
- The number of 
.in the IP address is not equal to \(3\); - Any octet in the IP address starts with 
0; - Any octet in the IP address is greater than \(255\).
 
Then we group the invalid IP addresses and count the occurrences of each invalid IP address invalid_count, and finally sort by invalid_count and ip in descending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  |  |