3451. Find Invalid IP Addresses LeetCode Solution
In this guide, you will get 3451. Find Invalid IP Addresses LeetCode Solution with the best time and space complexity. The solution to Find Invalid IP Addresses problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Invalid IP Addresses solution in C++
- Find Invalid IP Addresses solution in Java
- Find Invalid IP Addresses solution in Python
- Additional Resources
Problem Statement of Find Invalid IP Addresses
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 255 in any octet
Has leading zeros in any octet (like 01.02.03.04)
Has less or more than 4 octets
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.
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
3451. Find Invalid IP Addresses LeetCode Solution in C++
WITH
InvalidIPs AS (
SELECT ip
FROM Logs
WHERE
LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3
OR ip REGEXP '(^|\\.)0[0-9]'
OR ip REGEXP '(^|\\.)([0-9]{4,}|[3-9][0-9]{2}|2[6-9][0-9]|25[6-9])(\\.|$)'
)
SELECT ip, COUNT(*) AS invalid_count
FROM InvalidIPs
GROUP BY ip
ORDER BY invalid_count DESC, ip DESC;
/* code provided by PROGIEZ */
3451. Find Invalid IP Addresses LeetCode Solution in Java
N/A
// code provided by PROGIEZ
3451. Find Invalid IP Addresses LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.