LeetCode-- spell words

LeetCode-- spell words

Give you a "Glossary" (array of strings) words and an "alphabet" (string) chars.

If you can spell out words in a "word" (string) with the chars in the "letter" (character), then we think you've mastered the word.

Note: Each time the spelling, chars each letter can only be used once.

Return vocabulary words in all the words you have mastered the sum of the lengths.

Example 1:

Input: words = [ "cat", "bt", "hat", "tree"], chars = "atach"
Output: 6
Explanation:
may form a string "cat" and "hat", so the answer is 3 + 3 = 6.

Example 2:

Input: words = [ "hello", "world", "leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation:
may form a string "hello" and "world", so the answer is 5 + 5 = 10.

prompt:

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

Program: Each words are determined, the effective increase in length is calculated.

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

Others plan:

  • Thinking:
    only lowercase letters, so long hash count memory array 26 on it.
    chars character traversal, hash values ++
    words traverse, each character string traversed, temporary copy of the hash, each counting - it, directly break out = 0.
#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;
}

Guess you like

Origin www.cnblogs.com/beimangshanxiaoqigui/p/12511915.html