Description
You are given two 0-indexed arrays of strings startWords and targetWords. Each string consists of lowercase English letters only.
For each string in targetWords, check if it is possible to choose a string from startWords and perform a conversion operation on it to be equal to th
at from targetWords.
The conversion operation is described in the following two steps:
Append any lowercase letter that is not present in the string to its end.
For example, if the string is "abc", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be "abcd".
Rearrange the letters of the new string in any arbitrary order.
For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.
Return the number of strings in targetWords that can be obtained by performing the operations on any string of startWords.
Note that you will only be verifying if the string in targetWords can be obtained from a string in startWords by performing the operations. The strings in startWords do not actually change during this process.
Analysis
-
可以避免排序O(nlogn)再匹配带来的时间复杂度问题
-
用32位中的26位即可
Source Code
class Solution {
public:
int wordCount(vector<string>& startWords, vector<string>& targetWords) {
// 将 word 转化为表示包含字母状态的二进制整数
auto mask = [](string& word) -> int {
int res = 0;
for (char ch: word)
res |= 1 << (ch - 'a');
return res;
};
unordered_set<int> masks; // 所有可以获得的状态
for (string& start: startWords) {
// 遍历初始单词,根据其状态值构造所有可以获得的状态
int msk = mask(start);
for (int i = 0; i < 26; ++i)
if (((msk >> i) & 1) == 0)
masks.insert(msk | (1 << i));
}
int cnt = 0; // 可以获得的单词数
for (string& target: targetWords)
if (masks.count(mask(target)))
++cnt;
return cnt;
}
};