FastPrepCount Class-C IPv4 Addresses

Count Class-C IPv4 Addresses

Alchemy logoAlchemy● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given strings returned by an IP-list endpoint, count valid class-C IPv4 addresses and all valid IPv4 addresses.

An IPv4 address has exactly four decimal octets from 0 through 255, with no leading zero unless the octet is exactly 0. Under classful addressing, a valid address is class C when its first octet is from 192 through 223 inclusive.

Return [classCCount, validIPv4Count]. Ignore malformed strings.

Function

countClassCAddresses(addresses: String[]) → int[]

Examples

Example 1

addresses = ["192.168.1.1","223.0.0.1","10.0.0.1","256.1.1.1"]return = [2,3]

The first two valid addresses are class C; the 10.x address is valid but not class C.

Example 2

addresses = ["01.2.3.4","224.0.0.1","192.0.2.1"]return = [1,2]

Leading zeroes are invalid, 224 starts class D, and 192 starts class C.

Constraints

  • 0 <= addresses.length <= 100000.
  • Each string has length from 1 through 100.

More Alchemy problems

See Alchemy hiring insights
public int[] countClassCAddresses(String[] addresses) {
    // write your code here
}
addresses["192.168.1.1","223.0.0.1","10.0.0.1","256.1.1.1"]
expected[2,3]
Checking account…