【leetcode】最短完整词

时间:2020-09-06 14:27:24   收藏:0   阅读:57

 

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];
}

 

原文:https://www.cnblogs.com/ganxiang/p/13621022.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!