String
containing at least 4 numbers that represented an IPv4 Address, but the separator data - i.e. the dots that separate each Byte in a 4 Byte Ipv4 address, has been lost. Write a method - generateIPAddrs
that takes in this String
and returns an ArrayList
of Strings containing all possible IPv4 Addresses that can be generated from the given sequence of decimal integers.generateIPAddrs("1001")
==> {"1.0.0.1"}
generateIPAddrs("1010")
==> {"1.0.1.0"}
generateIPAddrs("25525511135")
==> {"255.255.11.135", "255.255.111.35"}
Stack
to perform a classic DFS
for this problem. Since we need to preserve some information as we build our stack, we'll create a local class
- IpLevelNode
that stores the IP Address generated to that point, as well as the level and remaining numbers available.
ArrayList
to store all the possible IP Addresses.local class
- IpLevelNode
that stores the IP Address generated to that point (predecessor
), as well as the level (level
) and remaining numbers available (successor
).Deque
as a stack.IpLevelNode
has no additional numbers remaining in its successor
, add the predecessor
of this node to the output ArrayList
.
public static ArrayList<String> generateIPAddrs(String s) { }
C
Java
Python