【leetcode】最短完整词

 

char * shortestCompletingWord(char * licensePlate, char ** words, int wordsSize){
    char* s = (char *)calloc(strlen(licensePlate),sizeof(char));
    int i,j;
    int n=0;
    int pst = -1;
    int len = 16;
    int flag = true;
    for (i=0; i<strlen(licensePlate); i++)
    {
        if (licensePlate[i]>='a' && licensePlate[i]<='z') s[n++] = licensePlate[i];
        else if(licensePlate[i]>='A' && licensePlate[i]<='Z') s[n++] = licensePlate[i] + 32;
    }
    for (i=0; i<wordsSize; i++)
    {
        if (strlen(words[i]) >= len) continue;
        int* hash = (int *)calloc(26,sizeof(int));
        for (j=0; j<strlen(words[i]); j++)
        {
            hash[words[i][j] - 97]++;
        }
        for (j=0; j<n; j++)
        {
            hash[s[j]-97]--;
            if (hash[s[j]-97]<0) 
            {
                flag = false;
                break;
            }
        }
        if(flag)
        {
            pst = i;
            len = strlen(words[i]);
        }
        flag = true;
    }
    return words[pst];
}

 

posted @ 2020-09-06 11:03  温暖了寂寞  阅读(99)  评论(0编辑  收藏  举报