Skip to content

Commit bab2dd2

Browse files
committed
task: #3451
1 parent 5602b16 commit bab2dd2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
194194
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
195195
- [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql)
196196
- [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
197+
- [3451. Find Invalid IP Addresses](./leetcode/hard/3451.%20Find%20Invalid%20IP%20Addresses.sql)
197198

198199
## Contributing
199200

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Question 3451. Find Invalid IP Addresses
3+
Link: https://leetcode.com/problems/find-invalid-ip-addresses/description/?envType=problem-list-v2&envId=database
4+
5+
Table: logs
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| log_id | int |
11+
| ip | varchar |
12+
| status_code | int |
13+
+-------------+---------+
14+
log_id is the unique key for this table.
15+
Each row contains server access log information including IP address and HTTP status code.
16+
Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:
17+
18+
Contains numbers greater than 255 in any octet
19+
Has leading zeros in any octet (like 01.02.03.04)
20+
Has less or more than 4 octets
21+
Return the result table ordered by invalid_count, ip in descending order respectively.
22+
*/
23+
24+
SELECT
25+
ip,
26+
COUNT(ip) AS invalid_count
27+
FROM logs
28+
WHERE
29+
ARRAY_LENGTH(STRING_TO_ARRAY(ip, '.'), 1) != 4
30+
OR LEFT(SPLIT_PART(ip, '.', 1), 1) = '0'
31+
OR LEFT(SPLIT_PART(ip, '.', 2), 1) = '0'
32+
OR LEFT(SPLIT_PART(ip, '.', 3), 1) = '0'
33+
OR LEFT(SPLIT_PART(ip, '.', 4), 1) = '0'
34+
OR SPLIT_PART(ip, '.', 1)::numeric > 255
35+
OR SPLIT_PART(ip, '.', 2)::numeric > 255
36+
OR SPLIT_PART(ip, '.', 3)::numeric > 255
37+
OR SPLIT_PART(ip, '.', 4)::numeric > 255
38+
GROUP BY ip
39+
ORDER BY invalid_count DESC, ip DESC

0 commit comments

Comments
 (0)