1
var restoreIpAddresses = function (s) {4
const go = (str, arr) => {5
// if we used every character and have 4 sections, it's a good IP!6
if (str.length === 0 && arr.length === 4) {7
results.push(arr.join("."));10
// we already have too many in the array, let's just stop11
if (arr.length >= 4) {14
// chop off next 3 characters and continue recursing17
+str.substring(0, 3) < 256 &&18
+str.substring(0, 3) > 0 &&21
go(str.slice(3), [...arr, str.substring(0, 3)]);23
// chop off next 2 characters and continue recursing24
if (str.length > 1 && +str.substring(0, 2) > 0 && str[0] !== "0") {25
go(str.slice(2), [...arr, str.substring(0, 2)]);27
// chop off next 1 character and continue recursing, in this case, starting with 0 is OK28
if (str.length > 0 && +str.substring(0, 1) >= 0) {29
go(str.slice(1), [...arr, str.substring(0, 1)]);