其他分享
首页 > 其他分享> > 【leetcode】两句话中的不常见单词

【leetcode】两句话中的不常见单词

作者:互联网

 

char ** uncommonFromSentences(char * A, char * B, int* returnSize){
    int alen = strlen(A);
    int blen = strlen(B);

    if (!alen && !blen)
    {
        * returnSize = 0;
        return NULL;
    }
    char* str = (char*)calloc(alen+blen+2,1); 
    if (alen)
    {
        strcpy(str,A);
        strcat(str," ");
        strcat(str,B);
    }
    else strcat(str, B);
    int len = strlen(str);
    char** arr = (char**)malloc(201*sizeof(char*));
    char* s[200] = {0};
    int hash[200] = {0};
    int index = 0;
    int flag = true;
    char* p = str;
    int n = 0;
    for (int i=0; i<len+1; i++)
    {
        if (str[i] == ' ' || str[i] == '\0')
        {
            str[i] = '\0';
            for(int j=0; j<=index; j++)
            {            
                if (s[j] && !strcmp(s[j],p))
                {
                    hash[j]++;
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                hash[index]++;
                s[index++] = p;
            }
            flag = true;
            p = &str[i+1];
        }
    }
    for (int j=0; j<index; j++)
    {
        if (hash[j]<2)
        {
            arr[n++] =  s[j];
        }
    }
    *returnSize = n;
    return arr;
}

 

标签:char,两句话,int,strcat,单词,alen,str,strlen,leetcode
来源: https://www.cnblogs.com/ganxiang/p/13582021.html