MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / backtracking

Function backtracking

javascript/0093-restore-ip-addresses.js:16–33  ·  view source on GitHub ↗

* * @param {number} i * @param {number} dots * @param {string} currentIP

(i, dots, currentIP)

Source from the content-addressed store, hash-verified

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

Callers 1

restoreIpAddressesFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected