🔒 Use constraints to solve with vector of size 677 🔢
Intuition\nThe problem seems to involve counting pairs of words in a given list of strings. Each word in the list is potentially paired with its reverse. The ob
# Intuition\nThe problem seems to involve counting pairs of words in a given list of strings. Each word in the list is potentially paired with its reverse. The objective is to find the maximum number of such pairs that can be formed.\n\n# Approach\n1. We\'ll iterate through the list of words, counting occurrences of each word and its reverse.\n2. To avoid counting duplicates (like palindromes), we\'ll check if the reversed word is different from the original word before counting it.\n3. We\'ll store the counts in a vector.\n4. After counting, we\'ll iterate through the counts vector and count the number of pairs that have occurred at least twice.\n5. Finally, we\'ll return half of this count, as each pair has been counted twice.\n\n# Complexity\n- Time complexity: \n  - Counting occurrences of each word and its reverse takes O(n), where n is the number of words.\n  - Iterating through the counts vector takes O(677), which is a constant time operation.\n  - Overall, the time complexity is O(n).\n\n- Space complexity:\n  - We use a vector to store counts, which has a fixed size of 677.\n  - The space complexity is therefore O(1), as it doesn\'t depend on the size of the input.\n\n# Code\n```cpp []\n#include <vector>\n#include <string>\n#include <algorithm> // for std::reverse\n\nclass Solution {\npublic:\n    int maximumNumberOfStringPairs(std::vector<std::string>& words) {\n        std::vector<int> ans(677, 0); // Initialize vector to store counts\n\n        // Count occurrences of each word and its reverse\n        for (int i = 0; i < words.size(); ++i) {\n            int index = stringtoindex(words[i]);\n            ans[index]++; // Increment count for original word\n\n            std::string reversed = words[i];\n            std::reverse(reversed.begin(), reversed.end()); // Reverse the word\n            if (reversed != words[i]) { // Avoid counting duplicates\n                ans[stringtoindex(reversed)]++; // Increment count for reversed word\n            }\n        }\n\n        int cnt = 0;\n        for (int i = 0; i < 677; ++i) {\n            if (ans[i] >= 2) {\n                cnt++; // Increment count for each pair found\n            }\n        }\n\n        return cnt/2;\n    }\n\nprivate:\n    int stringtoindex(const std::string& str) {\n        int index = (str[0] - \'a\') * 26 + (str[1] - \'a\');\n        return index;\n    }\n\n    std::string stringtoindex(int index) {\n        std::string str;\n        str.push_back(\'a\' + index / 26);\n        str.push_back(\'a\' + index % 26);\n        return str;\n    }\n};\n\n```
['Array', 'Hash Table', 'Hash Function', 'C++']