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
| function partition(s: string): string[] { const res = [], arr = [];
function isPalindrome(i: number, j: number) { for (; i < j; ) { if (s[i] !== s[j]) { return false; } i++; j--; } return true; }
function loop(left: number, right: number) { if (left === s.length) { return res.push(arr.slice()); } if (right > s.length) return; if (isPalindrome(left, right - 1)) { arr.push(s.slice(left, right)); loop(right, right + 1); arr.pop(); } loop(left, right + 1); }
loop(0, 1); return res; }
|