LeetCode——拼写单词

LeetCode——拼写单词

给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。

注意:每次拼写时,chars 中的每个字母都只能用一次。

返回词汇表 words 中你掌握的所有单词的 长度之和。

示例 1:

输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释:
可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。

示例 2:

输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。

提示:

1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
所有字符串中都仅包含小写英文字母

方案一:每个words都判断,有效则计算增加长度。

static int judgeCharacter(char *word, char *chars)
{
    //判断该单词是否掌握。
    char buf[100] = {0};
    char *p = NULL;
    //这个判断条件有问题,还暂时想不通。
    // if(word == NULL || strlen(word) == 0 || chars == NULL || strlen(chars))
    // {
    //     //单词长度为0, 字母表为0
    //     return -1;
    // }
    //存储chars备份
    strcpy(buf, chars);
    //轮询查找每一个字母是否匹配,找不到则返回-1.
    char *p_buf = word;
    while(*p_buf)
    {
        p = NULL;
        for(int i = 0; i < strlen(buf); i++)
        {
            if(buf[i] == *p_buf)
            {
                p = &buf[i];
                break;
            }
        }
        if(p == NULL)
        {
            //未找到
            return -1;
        }
        else
        {
            //找到则删除buf中该位置的字母,使用大写字母替代(因为单词只有小写字母,用大写X)
            *p = 'X';
            p_buf++;
        }
    }
    //都找到则返回word的长度
    return strlen(word);
}

int countCharacters(char ** words, int wordsSize, char * chars){
    int dstLenCount = 0;
    int tmpLen = 0;
    if(wordsSize <= 0)
    {
        return 0;
    }
    //遍历每个单词进行判断,匹配则将长度增加.
    for(int i = 0; i < wordsSize; i++)
    {
        tmpLen = judgeCharacter(words[i], chars);
        if(tmpLen < 0)
        {
            //匹配失败
            continue;
        }
        else
        {
            dstLenCount += tmpLen;
        }
    }
    return dstLenCount;
}

他人方案:

  • 思路:
    只有小写字母,因此长26的数组存hash计数就可以了。
    chars字符遍历,hash值++
    words遍历,每个字符串字符遍历,拷贝个临时hash,每个计数--就可以了,=0直接break出去。
#define HASH_SIZE (26)
#define GET_HASH_SUB(val) ((val) % HASH_SIZE)

int countCharacters(char ** words, int wordsSize, char * chars){
    int hash[HASH_SIZE];
    int tmpHash[HASH_SIZE];
    int len = 0;
    char *pChar = chars;
    int retLen = 0;
    int sub = 0;

    if(NULL == words
        || 0 == wordsSize
        || NULL == chars
        || 0 == strlen(chars)){
        return 0;
    }

    memset(hash, 0, sizeof(hash));
    while(*pChar != '\0'){
        hash[GET_HASH_SUB(*pChar)]++;
        pChar++;
    }

    for(int i = 0; i < wordsSize; i++){
        memcpy(tmpHash, hash, sizeof(hash));
        len = 0;
        pChar = words[i];
        while(*pChar != '\0'){
            sub = GET_HASH_SUB(*pChar);
            if(0 == tmpHash[sub]){
                break;
            }
            tmpHash[sub]--;
            pChar++;
            len++;
        }
        if(*pChar == '\0'){
            retLen += len;
        }
    }
    
    return retLen;
}

猜你喜欢

转载自www.cnblogs.com/beimangshanxiaoqigui/p/12511915.html