-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIPAddressCombinations.java
More file actions
47 lines (40 loc) · 1.5 KB
/
IPAddressCombinations.java
File metadata and controls
47 lines (40 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class IPAddressCombinations {
public List<String> findCombinations(String digits) {
return findCombinations(digits, 0, 0);
}
List<String> findCombinations(String digits, int index, int dots) {
if (dots >= 3) {
if (index >= digits.length())
return new ArrayList<>();
if (digits.charAt(index) == '0')
return new ArrayList<>();
int prefix = Integer.parseInt(digits.substring(index));
if (prefix < 256)
return new ArrayList<>(Arrays.asList(digits.substring(index)));
else
return new ArrayList<>();
}
List<String> ips = new ArrayList<>();
for (int i = index; i < index + 3 && i < digits.length(); i++) {
int prefix = Integer.parseInt(digits.substring(index, i + 1));
if (prefix < 256) {
List<String> subIps = findCombinations(digits, i + 1, dots + 1);
if (!subIps.isEmpty()) {
for (String subIp : subIps) {
ips.add("" + prefix + "." + subIp);
}
}
}
}
return ips;
}
public static void main(String[] args) {
IPAddressCombinations iac = new IPAddressCombinations();
String digits = "2542540123";
List<String> ips = iac.findCombinations(digits);
System.out.println(ips);
}
}