(s)
| 3 | * @return {string[]} |
| 4 | */ |
| 5 | var restoreIpAddresses = function (s) { |
| 6 | let res = []; |
| 7 | |
| 8 | if (s.length > 12) return res; |
| 9 | |
| 10 | /** |
| 11 | * |
| 12 | * @param {number} i |
| 13 | * @param {number} dots |
| 14 | * @param {string} currentIP |
| 15 | */ |
| 16 | function backtracking(i, dots, currentIP) { |
| 17 | if (dots === 4 && i == s.length) { |
| 18 | res.push(currentIP.slice(0, currentIP.length - 1)); |
| 19 | return; |
| 20 | } else if (dots > 4) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | for (let j = i; j < Math.min(i + 3, s.length); j++) { |
| 25 | if (+s.slice(i, j + 1) < 256 && (i == j || s[i] != '0')) { |
| 26 | backtracking( |
| 27 | j + 1, |
| 28 | dots + 1, |
| 29 | currentIP + s.slice(i, j + 1) + '.', |
| 30 | ); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | backtracking(0, 0, ''); |
| 36 | |
| 37 | return res; |
| 38 | }; |
nothing calls this directly
no test coverage detected