Problem · String

IPv4 Address Classification

Learn this problem
EasyRippling logoRipplingFULLTIMEOA

Problem statement

Given a string address, determine its class as an IPv4 address.

A valid address for this exercise has exactly four dot-separated parts. Every part must contain one to three decimal digits and have a numeric value from 0 through 255, inclusive. Leading zeros are allowed.

After validating all four parts, classify the address by the first part:

  • Return 1 for values from 1 through 126.
  • Return 2 for values from 128 through 191.
  • Return 3 for values from 192 through 223.
  • Return 4 for values from 224 through 239.
  • Return 5 for values from 240 through 255.

The first-part values 0 and 127 are reserved in this exercise. Return -1 for either reserved value or for any invalid address.

Function

classifyIpAddress(address: String) → int

Examples

Example 1

address = "10.20.30.40"return = 1

All four parts are valid, and the first part, 10, is in the range for class 1.

Example 2

address = "192.168.1.1"return = 3

The first part is 192, so the valid address belongs to class 3.

Example 3

address = "127.0.0.1"return = -1

Although every part is numeric and within the octet range, 127 is a reserved first-part value in this exercise.

Example 4

address = "10.20.300.40"return = -1

The third part is greater than 255, so the address is invalid.

Constraints

  • 1 <= address.length <= 100
  • address contains ASCII characters.

More Rippling problems

drafts saved locally
public int classifyIpAddress(String address) {
    // write your code here
}
address"10.20.30.40"
expected1
checking account