|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Hard |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3451. Find Invalid IP Addresses](https://leetcode.com/problems/find-invalid-ip-addresses) |
| 12 | + |
| 13 | +[中文文档](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code> logs</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+---------+ |
| 25 | +| log_id | int | |
| 26 | +| ip | varchar | |
| 27 | +| status_code | int | |
| 28 | ++-------------+---------+ |
| 29 | +log_id is the unique key for this table. |
| 30 | +Each row contains server access log information including IP address and HTTP status code. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p>Write a solution to find <strong>invalid IP addresses</strong>. An IPv4 address is invalid if it meets any of these conditions:</p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>Contains numbers <strong>greater than</strong> <code>255</code> in any octet</li> |
| 37 | + <li>Has <strong>leading zeros</strong> in any octet (like <code>01.02.03.04</code>)</li> |
| 38 | + <li>Has <strong>less or more</strong> than <code>4</code> octets</li> |
| 39 | +</ul> |
| 40 | + |
| 41 | +<p>Return <em>the result table </em><em>ordered by</em> <code>invalid_count</code>, <code>ip</code> <em>in <strong>descending</strong> order respectively</em>. </p> |
| 42 | + |
| 43 | +<p>The result format is in the following example.</p> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong class="example">Example:</strong></p> |
| 47 | + |
| 48 | +<div class="example-block"> |
| 49 | +<p><strong>Input:</strong></p> |
| 50 | + |
| 51 | +<p>logs table:</p> |
| 52 | + |
| 53 | +<pre class="example-io"> |
| 54 | ++--------+---------------+-------------+ |
| 55 | +| log_id | ip | status_code | |
| 56 | ++--------+---------------+-------------+ |
| 57 | +| 1 | 192.168.1.1 | 200 | |
| 58 | +| 2 | 256.1.2.3 | 404 | |
| 59 | +| 3 | 192.168.001.1 | 200 | |
| 60 | +| 4 | 192.168.1.1 | 200 | |
| 61 | +| 5 | 192.168.1 | 500 | |
| 62 | +| 6 | 256.1.2.3 | 404 | |
| 63 | +| 7 | 192.168.001.1 | 200 | |
| 64 | ++--------+---------------+-------------+ |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p><strong>Output:</strong></p> |
| 68 | + |
| 69 | +<pre class="example-io"> |
| 70 | ++---------------+--------------+ |
| 71 | +| ip | invalid_count| |
| 72 | ++---------------+--------------+ |
| 73 | +| 256.1.2.3 | 2 | |
| 74 | +| 192.168.001.1 | 2 | |
| 75 | +| 192.168.1 | 1 | |
| 76 | ++---------------+--------------+ |
| 77 | +</pre> |
| 78 | + |
| 79 | +<p><strong>Explanation:</strong></p> |
| 80 | + |
| 81 | +<ul> |
| 82 | + <li>256.1.2.3 is invalid because 256 > 255</li> |
| 83 | + <li>192.168.001.1 is invalid because of leading zeros</li> |
| 84 | + <li>192.168.1 is invalid because it has only 3 octets</li> |
| 85 | +</ul> |
| 86 | + |
| 87 | +<p>The output table is ordered by invalid_count, ip in descending order respectively.</p> |
| 88 | +</div> |
| 89 | + |
| 90 | +<!-- description:end --> |
| 91 | + |
| 92 | +## Solutions |
| 93 | + |
| 94 | +<!-- solution:start --> |
| 95 | + |
| 96 | +### Solution 1: Simulation |
| 97 | + |
| 98 | +We can determine if an IP address is invalid based on the following conditions: |
| 99 | + |
| 100 | +1. The number of `.` in the IP address is not equal to $3$; |
| 101 | +2. Any octet in the IP address starts with `0`; |
| 102 | +3. Any octet in the IP address is greater than $255$. |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +<!-- tabs:start --> |
| 107 | + |
| 108 | +#### MySQL |
| 109 | + |
| 110 | +```sql |
| 111 | +SELECT |
| 112 | + ip, |
| 113 | + COUNT(*) AS invalid_count |
| 114 | +FROM logs |
| 115 | +WHERE |
| 116 | + LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3 |
| 117 | + OR SUBSTRING_INDEX(ip, '.', 1) REGEXP '^0[0-9]' |
| 118 | + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) REGEXP '^0[0-9]' |
| 119 | + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) REGEXP '^0[0-9]' |
| 120 | + OR SUBSTRING_INDEX(ip, '.', -1) REGEXP '^0[0-9]' |
| 121 | + OR SUBSTRING_INDEX(ip, '.', 1) > 255 |
| 122 | + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) > 255 |
| 123 | + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) > 255 |
| 124 | + OR SUBSTRING_INDEX(ip, '.', -1) > 255 |
| 125 | +GROUP BY 1 |
| 126 | +ORDER BY 2 DESC, 1 DESC; |
| 127 | +``` |
| 128 | + |
| 129 | +#### Pandas |
| 130 | + |
| 131 | +```python |
| 132 | +import pandas as pd |
| 133 | + |
| 134 | + |
| 135 | +def find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame: |
| 136 | + def is_valid_ip(ip: str) -> bool: |
| 137 | + octets = ip.split(".") |
| 138 | + if len(octets) != 4: |
| 139 | + return False |
| 140 | + for octet in octets: |
| 141 | + if not octet.isdigit(): |
| 142 | + return False |
| 143 | + value = int(octet) |
| 144 | + if not 0 <= value <= 255 or octet != str(value): |
| 145 | + return False |
| 146 | + return True |
| 147 | + |
| 148 | + logs["is_valid"] = logs["ip"].apply(is_valid_ip) |
| 149 | + invalid_ips = logs[~logs["is_valid"]] |
| 150 | + invalid_count = invalid_ips["ip"].value_counts().reset_index() |
| 151 | + invalid_count.columns = ["ip", "invalid_count"] |
| 152 | + result = invalid_count.sort_values( |
| 153 | + by=["invalid_count", "ip"], ascending=[False, False] |
| 154 | + ) |
| 155 | + return result |
| 156 | +``` |
| 157 | + |
| 158 | +<!-- tabs:end --> |
| 159 | + |
| 160 | +<!-- solution:end --> |
| 161 | + |
| 162 | +<!-- problem:end --> |
0 commit comments