Skip to content

Practice Find Invalid IP Addresses

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3451
LeetCode Problem

Find Invalid IP Addresses

Problem Statement

Table logs has log_id INT, ip VARCHAR, and status_code INT. An address is invalid if its dot-separated octets do not number four, any octet exceeds 255, or a multi-digit octet begins with zero. Each supplied octet has one to three digits. Count log rows for each invalid textual address and return ip and invalid_count, ordered by count descending and then ip descending. Status code does not affect validation. Practice approach: Validate the four-part structure and every textual octet before numeric conversion erases leading zeros. Group only invalid rows by the original address. A single zero is valid; 00 is not.

Source: Find Invalid IP Addresses on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
{
  "logs": [
    {
      "log_id": 1,
      "ip": "1.2.3.4",
      "status_code": 200
    },
    {
      "log_id": 2,
      "ip": "01.2.3.4",
      "status_code": 200
    },
    {
      "log_id": 3,
      "ip": "01.2.3.4",
      "status_code": 500
    },
    {
      "log_id": 4,
      "ip": "256.2.3.4",
      "status_code": 200
    },
    {
      "log_id": 5,
      "ip": "1.2.3",
      "status_code": 404
    },
    {
      "log_id": 6,
      "ip": "0.0.0.0",
      "status_code": 200
    }
  ]
}
Output
[
  {
    "ip": "01.2.3.4",
    "invalid_count": 2
  },
  {
    "ip": "256.2.3.4",
    "invalid_count": 1
  },
  {
    "ip": "1.2.3",
    "invalid_count": 1
  }
]
Explanation

The leading-zero address occurs twice. The other failures have an oversized octet or only three octets.

Constraints

log_id is unique.
Each input octet contains one to three digits.

Related LeetCode Problems

Practice more problems similar to Find Invalid IP Addresses.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.