--- tags: leetcode --- # [2135. Count Words Obtained After Adding a Letter](https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/) --- # My Solution ## The Key Idea for Solving This Coding Question ## C++ Code ```cpp= class Solution { public: int wordCount(vector<string> &startWords, vector<string> &targetWords) { unordered_set<unsigned int> hashedStartWords; for (auto &startWord : startWords) { hashedStartWords.insert(hashString(startWord)); } int machedCnt = 0; for (auto &targetWord : targetWords) { for (int skip = 0; skip < targetWord.size(); ++skip) { unsigned int hashedTargetWord = hashString2(targetWord, skip); if (hashedStartWords.find(hashedTargetWord) != hashedStartWords.end()) { ++machedCnt; break; } } } return machedCnt; } private: unsigned int hashString(string &s) { unsigned int hash = 0; for (auto &c : s) { hash = hash + (1 << (c - 'a')); } return hash; } unsigned int hashString2(string &s, int skip) { unsigned int hash = 0; for (int i = 0; i < s.size(); ++i) { if (i == skip) { continue; } hash = hash + (1 << (s[i] - 'a')); } return hash; } }; ``` ## Time Complexity ## Space Complexity # Miscellane <!-- # Test Cases ``` ["ant","act","tack"] ["tack","act","acti"] ``` ``` ["ab","a"] ["abc","abcd"] ``` * Wrong Answer ``` ["g","vf","ylpuk","nyf","gdj","j","fyqzg","sizec"] ["r","am","jg","umhjo","fov","lujy","b","uz","y"] ``` * Time Limit Exceeded https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/submissions/839285643/ * Time Limit Exceeded https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/submissions/847342898/ -->