https://leetcode.com/problems/longest-common-prefix


Accepted


char* longestCommonPrefix(char** strs, int strsSize) {
    int common_char_index = 0;
    while (true) {
        bool ok = true;
        char *the_char = NULL;

        int index = 0;
        while (true) {
            char *current_string = strs[index];
            if (common_char_index >= strlen(current_string)) {
                ok = false;
                break;
            }
            if (the_char == NULL) {
                the_char = current_string[common_char_index];
                //printf("%c", the_char);
            }
            if (current_string[common_char_index] != the_char) {
                ok = false;
                break;
            }
            index += 1;
            if (index >= strsSize) {
                break;
            }
        }

        if (ok == false) {
            break;
        }

        common_char_index += 1;
    }

    char *common_string = malloc(sizeof(char) * 200);
    for (int i = 0; i <= common_char_index - 1; i = i + 1) {
        //printf("%c", strs[0][i]);
        common_string[i] = (char)strs[0][i];
    }
    common_string[common_char_index] =  '\0';
    return common_string;
}
