-
Notifications
You must be signed in to change notification settings - Fork 91
/
Restore_IP_Addresses.js
51 lines (39 loc) · 1.18 KB
/
Restore_IP_Addresses.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
/*
Restore IP Addresses
https://leetcode.com/problems/restore-ip-addresses/description/
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
*/
var restoreIpAddresses = function (s) {
var restore = restoreInputBits("", s, 4);
var ret = [];
for (var i = 0; i < restore.length; i++) {
ret.push(restore[i].join("."));
}
return ret;
};
var restoreInputBits = function (partial, s, num) {
if (s.length == 0 && num == 0) return [partial];
if (s.length < num || s.length > num * 3 || num == 0) return [];
const oneNum = restoreInputBits(
[...partial, s.slice(0, 1)],
s.slice(1),
num - 1
);
if (s.length === 1 || s.slice(0, 1) === "0") return oneNum;
const twoNums = restoreInputBits(
[...partial, s.slice(0, 2)],
s.slice(2),
num - 1
);
if (s.length === 2 || s.slice(0, 3) > 255) return [...oneNum, ...twoNums];
const threeNums = restoreInputBits(
[...partial, s.slice(0, 3)],
s.slice(3),
num - 1
);
return [...oneNum, ...twoNums, ...threeNums];
};
module.exports.restoreIpAddresses = restoreIpAddresses;