【leetcode】最常见的单词

 

/*字符串升序快排 每个字母都比较*/
int cmp(const void* a, const void* b){
    char* s1 = *(char**)a;
    char* s2 = *(char**)b;
    while(*s1 == *s2 && *s1 && *s2) 
        s1++,s2++;
    return *s1 - *s2;
}

void tolow(char* s){
    for (int i=0; s[i] != '\0'; i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] +=32;
}
char * mostCommonWord(char * paragraph, char ** banned, int bannedSize){
    char* str[500],*buffer;
    int i,slen=0,max=0,count=1;
    bool flag1 =true;
    tolow(paragraph);
    buffer = strtok(paragraph," !?',;.");
    while(buffer)
    {
        for (i=0; i<bannedSize; i++)
        {
            if (!strcmp(buffer,banned[i]))
            {
                flag1 = false;
                break;
            }
        }            
        if (flag1) str[slen++] = buffer;
        flag1 = true;
        buffer = strtok(NULL," !?',;.");
    }
    qsort(str,slen,sizeof(char*),cmp);
    for (i=1; i<slen; i++)
    {
        if (strcmp(str[i],str[i-1]))
        {
            if (count > max) 
            {
                str[0] = str[i-1];
                max=count;
            }            
            count=0;
        }
        count++;
    }
    return (count > max)? str[slen-1]: str[0];
}

 

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