Likou 131. Split palindrome-C language implementation-medium difficulty

Likou 131. Split palindrome-C language implementation-medium difficulty

topic

Portal

text

Given a string s, split s into some substrings so that each substring is a palindrome.

Return all possible splitting schemes for s.

Example:

Input: "aab" Output: [["aa","b"], ["a","a","b"]]

Source: LeetCode

template

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 *  * returnSize  
 * * returnColumnSizes  
 *  * columnSizes free 
 */
char *** partition(char * s, int* returnSize, int** returnColumnSizes){

}
 

Problem solving

analysis of idea

We can know that we need to analyze all possible split methods. It is very troublesome to use C language to solve problems.

Complete source code

void dfs(char* s, int n, int i, int** f, char*** ret, int* retSize, int* retColSize, char** ans, int* ansSize) {
    if (i == n) {
        char** tmp = malloc(sizeof(char*) * (*ansSize));//malloc 
        for (int j = 0; j < (*ansSize); j++) {
            int ansColSize = strlen(ans[j]);
            tmp[j] = malloc(sizeof(char) * (ansColSize + 1));
            strcpy(tmp[j], ans[j]);
        }
        ret[*retSize] = tmp;
        retColSize[(*retSize)++] = *ansSize;
        return;
    }
    for (int j = i; j < n; ++j) {
        if (f[i][j]) {
            char* sub = malloc(sizeof(char) * (j - i + 2));
            for (int k = i; k <= j; k++) {
                sub[k - i] = s[k];
            }
            sub[j - i + 1] = '\0';
            ans[(*ansSize)++] = sub;
            dfs(s, n, j + 1, f, ret, retSize, retColSize, ans, ansSize);
            --(*ansSize);
        }
    }
}

char*** partition(char* s, int* returnSize, int** returnColumnSizes) {
    int n = strlen(s);//s 
    int retMaxLen = n * (1 << n);//
    char*** ret = malloc(sizeof(char**) * retMaxLen);//malloc 
    *returnSize = 0;//0 
    *returnColumnSizes = malloc(sizeof(int) * retMaxLen);//malloc 
    int* f[n];//f n 
    for (int i = 0; i < n; i++) {
        f[i] = malloc(sizeof(int) * n);//
        for (int j = 0; j < n; j++) {
            f[i][j] = 1;//1 
        }
    }
    for (int i = n - 1; i >= 0; --i) {
        for (int j = i + 1; j < n; ++j) {
            f[i][j] = (s[i] == s[j]) && f[i + 1][j - 1];
        }
    }
    char* ans[n];//ans 
    int ansSize = 0;//ansSize
    dfs(s, n, 0, f, ret, returnSize, *returnColumnSizes, ans, &ansSize);//dfs 
    return ret;
}
 

operation result