-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRestoreIPAddresses.js
71 lines (64 loc) · 2.37 KB
/
RestoreIPAddresses.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @author Anirudh Sharma
*
* Given a string s containing only digits, return all possible valid IP addresses that
* can be obtained from s. You can return them in any order.
*
* A valid IP address consists of exactly four integers, each integer is between 0 and 255,
* separated by single dots and cannot have leading zeros. For example, "0.1.2.201" and
* "192.168.1.1" are valid IP addresses and "0.011.255.245", "192.168.1.312" and "[email protected]"
* are invalid IP addresses.
*
* Constraints:
*
* 0 <= s.length <= 3000
* s consists of digits only
*/
const restoreIpAddresses = (s) => {
// List to store all the IP addresses
const ipAddresses = [];
// Array to store four parts of the IP address
const ipAddressParts = Array(4);
// Call the helper function to get the IP addresses
getIPAddresses(s, ipAddresses, 0, ipAddressParts, 0);
return ipAddresses;
};
const getIPAddresses = (s, ipAddresses, currentIndex, ipAddressParts, segment) => {
// Base case when the current index is at the end of
// the string and we have found all four segments
if (segment === 4 && currentIndex === s.length) {
ipAddresses.push(ipAddressParts[0] + "." + ipAddressParts[1] + "." + ipAddressParts[2] + "." + ipAddressParts[3]);
return;
}
// If either of the above conditions is met but not both
else if (segment === 4 || currentIndex === s.length) {
return;
}
// Loop through the string to find the segments
for (let i = 1; i <= 3 && currentIndex + i <= s.length; i++) {
// Get the current segment of string
let value = parseInt(s.substring(currentIndex, currentIndex + i));
// Check for the validity of value
if (value > 255 || i >= 2 && s.charAt(currentIndex) === '0') {
break;
}
// If valid, add it to the segment
ipAddressParts[segment] = value;
// Recurse for the next part of the string
getIPAddresses(s, ipAddresses, currentIndex + i, ipAddressParts, segment + 1);
ipAddressParts[segment] = -1;
}
};
const main = () => {
let s = "25525511135";
console.log(restoreIpAddresses(s));
s = "0000";
console.log(restoreIpAddresses(s));
s = "1111";
console.log(restoreIpAddresses(s));
s = "010010";
console.log(restoreIpAddresses(s));
s = "101023";
console.log(restoreIpAddresses(s));
};
main();