Fast 0ms C solution


  • 0
    D

    int map[] = {2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, 3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3};

    int get_line(char ch)
    {
    if(ch >= 'a' && ch <= 'z')
    return map[ch - 'a'];
    else
    return map[ch - 'A'];
    }

    char** findWords(char** words, int wordsSize, int* returnSize) {
    int i = 0, j = 0;
    char **ret_arr = malloc(sizeof(char *) * wordsSize);

    *returnSize = 0;
    for(i = 0; i < wordsSize; i++)
    {
        int word_size = strlen(words[i]);
        int line = get_line(words[i][0]);
        int check = 1;
        for(j = 1; j < word_size; j++)
        {
            if(line != get_line(words[i][j]))
            {
                check = 0;
                break;
            }
        }
        if(check)
        {
            ret_arr[*returnSize] = malloc(sizeof(char) * (word_size + 1));
            snprintf(ret_arr[*returnSize], word_size + 1, "%s", words[i]);
            (*returnSize)++;
        }
    }
    
    return ret_arr;
    

    }


Log in to reply
 

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