Appearance
131.分割回文串
题解
js
function isPalindrome(s) {
for (let i = 0; i < s.length; i++) {
if (s[i] != s[s.length - i - 1]) {
return false;
}
}
return true;
}
/**
* @param {string} s
* @return {string[][]}
*/
var partition = function (s) {
const length = s.length;
const res = [];
function fn(s, arr, len) {
if (len === length) {
res.push([...arr]);
return;
}
for (let i = 0; i < s.length; i++) {
const cur = s.slice(0, i + 1);
console.log(cur);
const next = s.slice(i + 1);
if (isPalindrome(cur)) {
fn(next, arr.concat(cur), len + cur.length);
} else {
continue;
}
}
}
fn(s, [], 0);
return res;
};
function isPalindrome(s) {
for (let i = 0; i < s.length; i++) {
if (s[i] != s[s.length - i - 1]) {
return false;
}
}
return true;
}
/**
* @param {string} s
* @return {string[][]}
*/
var partition = function (s) {
const length = s.length;
const res = [];
function fn(s, arr, len) {
if (len === length) {
res.push([...arr]);
return;
}
for (let i = 0; i < s.length; i++) {
const cur = s.slice(0, i + 1);
console.log(cur);
const next = s.slice(i + 1);
if (isPalindrome(cur)) {
fn(next, arr.concat(cur), len + cur.length);
} else {
continue;
}
}
}
fn(s, [], 0);
return res;
};