1323. Octet Address Sort
A network log stores IPv4 addresses as text in the form a.b.c.d, where each of the four octets is a number from 0 to 255 written without leading zeros. Text order is misleading here, since "9.0.0.1" would come after "10.0.0.1" alphabetically. Given the array addresses, return the same strings ordered by numeric value, comparing the first octet first, then the second, and so on. Equal addresses stay adjacent, and being identical strings their order cannot be observed.
The intended method is least-significant-digit radix sort over the octets: perform four stable counting-sort passes with 256 buckets, starting from the last octet and ending with the first. That takes O(n) time for the fixed four octets plus O(256) per pass, and O(n) extra space.
Example 1
- Input:
- addresses = ["10.0.0.5","9.255.1.1","10.0.0.1"]
- Output:
- ["9.255.1.1","10.0.0.1","10.0.0.5"]
- Explanation:
Numerically 9.255.1.1 is below 10.0.0.1, which is below 10.0.0.5, although text order would put 9.255.1.1 last.
Example 2
- Input:
- addresses = ["192.168.1.20","192.168.1.3","192.167.200.9","8.8.8.8"]
- Output:
- ["8.8.8.8","192.167.200.9","192.168.1.3","192.168.1.20"]
- Explanation:
The first octets are 192, 192, 192, 8, and among the three 192 addresses the second octet 167 comes before 168, then the last octet 3 before 20.
Example 3
- Input:
- addresses = ["5.5.5.5","5.5.5.5","0.0.0.0"]
- Output:
- ["0.0.0.0","5.5.5.5","5.5.5.5"]
- Explanation:
0.0.0.0 is the smallest value and the two identical 5.5.5.5 addresses stay together.
Constraints
- 1 ≤ n ≤ 200
- Every address is a valid dotted quad
a.b.c.dwith each octet in 0..255 and no leading zeros
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)