LeetCode 5978. Count the number of words that can be obtained by adding letters (bit operation + hash)

Posted by eits on Sun, 09 Jan 2022 15:16:12 +0100

1. Title

Give you two string arrays with subscripts starting from 0, startWords and targetWords. Each string consists of only lowercase English letters.

For each string in targetWords, check whether a string can be selected from startWords, perform a conversion operation, and the result is equal to the current targetWords string.

The conversion operation is described in the following two steps:

  • Appends any lowercase letter that does not exist in the current string to the end of the current string.
    For example, if the string is "abc", the letters'd ',' e 'or' y 'can be added to the end of the string, but' a 'cannot. If'd 'is appended, the result string is "abcd".
  • Rearrange the letters in the new string. You can rearrange the letters in any order.
    For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.

Find out how many strings in targetWords can be obtained by performing the above conversion operation on any string in startWords. Returns the number of such strings in targetWords.

Note: you can only verify whether the string in targetWords can be obtained by performing an operation on a string in startWords. The string in startWords does not actually change during this process.

Example 1:
Input: startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
Output: 2
 Explanation:
- In order to form targetWords[0] = "tack" ,Can choose startWords[1] = "act" ,Append letter 'k' ,And rearrange "actk" by "tack" . 
- startWords There are no available to get targetWords[1] = "act" String of.
  be careful "act" Does exist in startWords ,However, you must append a letter to the string before rearrangement.
- In order to form targetWords[2] = "acti" ,Can choose startWords[1] = "act" ,Append letter 'i' ,And rearrange "acti" by "acti" oneself.

Example 2:
Input: startWords = ["ab","a"], targetWords = ["abc","abcd"]
Output: 1
 Explanation:
- In order to form targetWords[0] = "abc" ,Can choose startWords[0] = "ab" ,Append letter 'c' ,And rearrange as "abc" . 
- startWords There are no available to get targetWords[1] = "abcd" String of.
 
Tips:
1 <= startWords.length, targetWords.length <= 5 * 10^4
1 <= startWords[i].length, targetWords[j].length <= 26
startWords and targetWords Each string in consists of only lowercase English letters
 stay startWords or targetWords Each letter appears at most once in any string of

Source: LeetCode
Link: https://leetcode-cn.com/problems/count-words-obtained-after-adding-a-letter
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

2. Problem solving

  • Turn the word in startwords into a 26 bit int number, add a non-existent bit, and store all the information in the hash
  • Traverse the words in targetword into int, which can be found in the hash
class Solution {
public:
    int wordCount(vector<string>& startWords, vector<string>& targetWords) {
        unordered_set<int> set;
        for(auto& s : startWords)
        {
            int num = 0;
            for(auto c : s)
                num |= 1<<(c-'a');
            for(int i = 0; i < 26; ++i)
            {
                if((num&(1<<i))==0)//Added a letter that does not exist
                {
                    set.insert(num | (1<<i));
                }
            }
        }
        int ans = 0;
        for(auto& s : targetWords)
        {
            int num = 0;
            for(auto c : s)
                num |= 1<<(c-'a');
            if(set.find(num) != set.end())
                ans++;
        }
        return ans;
    }
};

460 ms 151.5 MB C++

My CSDN Blog address https://michael.blog.csdn.net/

Long press or sweep code pay attention to my official account (Michael Amin), together refueling, learning progress together!

Topics: leetcode