131. 分割回文串【中等题】

题目链接

const isSpecial = (s: string) => {
    let flag = true;
    for(let i=0,j=s.length-1;i<j;i++,j--) {
        if (s[i] !== s[j]) {
            flag=false;
            break;
        }
    }
    return flag;
};

function partition(s: string): string[][] {
    const res: string[][] = [];
    const splitStr = (start: number, curRes: string[], rawStr: string) => {
        // console.log('curRes', curRes);
        if(start>=rawStr.length)  {
            const newRes = [...curRes];
            res.push(newRes);
            return;
        }
        for(let next = start+1; next<=rawStr.length;next++) {
            const curPair = rawStr.slice(start, next);
            if(isSpecial(curPair)) {
                curRes.push(curPair);
                splitStr(next, curRes, rawStr);
                curRes.pop();
            } else {
                continue;
            }
        }
    };
    splitStr(0, [], s);
    return res;
};

results matching ""

    No results matching ""