[C++|Java|Python3] frequency table + mask
Please pull this commit for solutions of weekly 324. \n\nIntuition\nHere, we can use a mask to indicate what characters appear in a word, and use a hash table t
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/af6e415cd101768ea8743ea9e4d22d788c9461c3) for solutions of weekly 324. \n\n**Intuition**\nHere, we can use a mask to indicate what characters appear in a word, and use a hash table to indicate the frequency of a mask. \n**Implementation**\n**C++**\n```\nclass Solution {\npublic: \n\tint similarPairs(vector<string>& words) {\n\t\tint ans = 0; \n\t\tunordered_map<int, int> freq; \n\t\tfor (auto& word : words) {\n\t\t\tint mask = 0; \n\t\t\tfor (auto& c : word) mask |= 1 << (c-\'a\'); \n\t\t\tans += freq[mask]++; \n\t\t}\n\t\treturn ans; \n\t}\n};\n```\n**Java**\n```\nclass Solution {\n\tpublic int similarPairs(String[] words) {\n\t\tint ans = 0; \n\t\tHashMap<Integer, Integer> freq = new HashMap(); \n\t\tfor (var word : words) {\n\t\t\tint mask = 0; \n\t\t\tfor (var ch : word.toCharArray()) \n\t\t\t\tmask |= 1<<ch-\'a\'; \n\t\t\tans += freq.getOrDefault(mask, 0); \n\t\t\tfreq.merge(mask, 1, Integer::sum); \n\t\t}\n\t\treturn ans; \n\t}\n}\n```\n**Python3**\n```\nclass Solution: \n\tdef similarPairs(self, words: List[str]) -> int: \n\t\tans = 0\n\t\tfreq = Counter()\n\t\tfor word in words: \n\t\t\tmask = reduce(or_, (1<<ord(ch)-97 for ch in word))\n\t\t\tans += freq[mask]\n\t\t\tfreq[mask] += 1\n\t\treturn ans\n```\n**Complexity**\nTime O(NW) \nSpace O(N) \n
['C', 'Java', 'Python3']