Clean C Solution


  • 0
    L

    You need to count the amount of flips first, to know how big the list of strings will be.

    char** generatePossibleNextMoves(char* s, int* returnSize) {
        int count = 0;
        int s_len = 0;
        
        for (int c = 0; s[c] != '\0'; c++) {
            if (s[c] == '+' && s[c + 1] == '+')
                count++;
            
            s_len++;
        }
        
        char ** ret = (char **) calloc (count, sizeof (char *));
        count = 0; 
        for (int c = 0; s[c] != '\0'; c++) {
            if (s[c] == '+' && s[c + 1] == '+') {
                ret[count] = calloc (s_len + 1, sizeof (char));
                strcpy (ret[count], s);
                ret[count][c] = ret[count][c + 1] =  '-';
                count++;
            }
        }
        
        *returnSize = count;
        return ret;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.