1178. Number of Valid Words for Each Puzzle

With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:

  • word contains the first letter of puzzle.
  • For each letter in word, that letter is in puzzle.
    • For example, if the puzzle is “abcdefg”, then valid words are “faced”, “cabbage”, and “baggage”, while
    • invalid words are “beefed” (does not include ‘a’) and “based” (includes ‘s’ which is not in the puzzle).

Return an array answer, where answer[i] is the number of words in the given word list words that is valid with respect to the puzzle puzzles[i].
 

Example 1:

Input: words = [“aaaa”,“asas”,“able”,“ability”,“actt”,“actor”,“access”], puzzles = [“aboveyz”,“abrodyz”,“abslute”,“absoryz”,“actresz”,“gaswxyz”]
Output: [1,1,3,2,4,0]
Explanation:
1 valid word for “aboveyz” : “aaaa”
1 valid word for “abrodyz” : “aaaa”
3 valid words for “abslute” : “aaaa”, “asas”, “able”
2 valid words for “absoryz” : “aaaa”, “asas”
4 valid words for “actresz” : “aaaa”, “asas”, “actt”, “access”
There are no valid words for “gaswxyz” cause none of the words in the list contains letter ‘g’.

Example 2:

Input: words = [“apple”,“pleas”,“please”], puzzles = [“aelwxyz”,“aelpxyz”,“aelpsxy”,“saelpxy”,“xaelpsy”]
Output: [0,1,3,2,0]

Constraints:
  • 1 < = w o r d s . l e n g t h < = 10 5 1 <= words.length <= 10^5 1<=words.length<=105
  • 4 <= words[i].length <= 50
  • 1 < = p u z z l e s . l e n g t h < = 10 4 1 <= puzzles.length <= 10^4 1<=puzzles.length<=104
  • puzzles[i].length == 7
  • words[i] and puzzles[i] consist of lowercase English letters.
  • Each puzzles[i] does not contain repeated characters.

From: LeetCode
Link: 1178. Number of Valid Words for Each Puzzle


Solution:

Ideas:

convert each word/puzzle to a 26-bit mask, count word masks, then enumerate all subsets of each puzzle mask that include the first letter.

Code:
#include <stdlib.h>
#include <string.h>

#define HASH_SIZE 262144

typedef struct Node {
    int key;
    int count;
    struct Node* next;
} Node;

int getMask(char* s) {
    int mask = 0;
    for (int i = 0; s[i]; i++) {
        mask |= 1 << (s[i] - 'a');
    }
    return mask;
}

int hash(int key) {
    return key & (HASH_SIZE - 1);
}

void add(Node** table, int key) {
    int h = hash(key);
    Node* cur = table[h];

    while (cur) {
        if (cur->key == key) {
            cur->count++;
            return;
        }
        cur = cur->next;
    }

    Node* node = (Node*)malloc(sizeof(Node));
    node->key = key;
    node->count = 1;
    node->next = table[h];
    table[h] = node;
}

int find(Node** table, int key) {
    int h = hash(key);
    Node* cur = table[h];

    while (cur) {
        if (cur->key == key) return cur->count;
        cur = cur->next;
    }

    return 0;
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findNumOfValidWords(char** words, int wordsSize, char** puzzles, int puzzlesSize, int* returnSize) {
    Node** table = (Node**)calloc(HASH_SIZE, sizeof(Node*));

    for (int i = 0; i < wordsSize; i++) {
        int mask = getMask(words[i]);

        int bits = 0;
        int temp = mask;
        while (temp) {
            bits++;
            temp &= temp - 1;
        }

        if (bits <= 7) {
            add(table, mask);
        }
    }

    int* ans = (int*)malloc(sizeof(int) * puzzlesSize);
    *returnSize = puzzlesSize;

    for (int i = 0; i < puzzlesSize; i++) {
        int puzzleMask = getMask(puzzles[i]);
        int firstBit = 1 << (puzzles[i][0] - 'a');

        int count = 0;
        int sub = puzzleMask;

        while (sub) {
            if (sub & firstBit) {
                count += find(table, sub);
            }
            sub = (sub - 1) & puzzleMask;
        }

        ans[i] = count;
    }

    return ans;
}

更多推荐