首页 > 其他 > 详细

【leetcode】最短完整词

时间:2020-09-06 14:27:24      阅读:56      评论:0      收藏:0      [点我收藏+]

 

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

 

【leetcode】最短完整词

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!