Connection Status:
Competition Arena > IPConverter
SRM 210 · 2004-09-01 · by the_one_smiley · Recursion, Search, Sorting
Class Name: IPConverter
Return Type: String[]
Method Name: possibleAddresses
Arg Types: (string)
Problem Statement

Problem Statement

A computer connected to the internet is identified by an IP address. The most common way of displaying an IP address is the dotted quad method: four eight-bit (0-255 in base ten) numbers separated by periods.

Someone has given you a possible IP address, but the periods have been removed, leaving only a string of digits. Write a class IPConverter with a method possibleAddresses that takes a String ambiguousIP containing the digits and returns a String[] containing all the possible IP addresses that can be formed from those digits by inserting three periods to form a dotted quad. Sort the elements of the return value lexicographically, using their string ordering (the period character precedes all digit characters).

The numbers in each of the four positions can have any integer value between zero and 255, inclusive. However, a number may not have leading zeroes. For example, the digits 1902426 can form 1.90.24.26, 19.0.24.26, 190.2.4.26, and other IP addresses (see Example 0). However, it cannot form 19.02.4.26.

Constraints

  • ambiguousIP will contain between 0 and 50 characters, inclusive.
  • Each character of ambiguousIP will be between '0' and '9', inclusive.
Examples
0)
"1902426"
Returns: { "1.90.24.26",  "1.90.242.6",  "19.0.24.26",  "19.0.242.6",  "190.2.4.26",  "190.2.42.6",  "190.24.2.6" }

This is the example from the problem statement.

1)
"12345678"
Returns: { "1.234.56.78",  "12.34.56.78",  "123.4.56.78",  "123.45.6.78",  "123.45.67.8" }
2)
"255255255255"
Returns: { "255.255.255.255" }
3)
"000"
Returns: { }
4)
"0000"
Returns: { "0.0.0.0" }

Submissions are judged against all 71 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class IPConverter with a public method vector<string> possibleAddresses(string ambiguousIP) · 71 test cases · 2 s / 256 MB per case

Submitting as anonymous