[C++] Brutal Force
Intuition\nThe key insight is to focus on the differences in their digit arrays and determine if these differences can be reconciled with the allowed swaps.\n\n
# Intuition\nThe key insight is to focus on the differences in their digit arrays and determine if these differences can be reconciled with the allowed swaps.\n\n# Approach\n- **Preprocess Digits**: Convert each number in the array into an array of its digits for easier comparison.\n- **Check Almost Equal**: Implement a function to check if two digit arrays are almost equal by analyzing the number and positions of differing digits, allowing for at most two swaps to reconcile the differences.\n\n# Complexity\n- Time complexity: $O(n^2)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nbool almost_equal(const array<int, 8> &a, const array<int, 8> &b) {\n    array<pair<int, int>, 8> differentPairs\n    int diffCount = 0;\n\n    for (int i = 0; i < 8; i++) {\n        if (a[i] != b[i]) {\n            if (diffCount >= 4) {\n                return false;\n            }\n            differentPairs[diffCount++] = {a[i], b[i]};\n        }\n    }\n\n    if (diffCount == 1) {\n        return false;\n    }\n    if (diffCount == 2) {\n        return (differentPairs[0].first == differentPairs[1].second &&\n                differentPairs[0].second == differentPairs[1].first);\n    }\n    if (diffCount == 3) {\n        return (differentPairs[1].first == differentPairs[0].second &&\n                differentPairs[2].first == differentPairs[1].second &&\n                differentPairs[0].first == differentPairs[2].second) ||\n               (differentPairs[2].first == differentPairs[0].second &&\n                differentPairs[0].first == differentPairs[1].second &&\n                differentPairs[1].first == differentPairs[2].second);\n    }\n    if (diffCount == 4) {\n        return ((differentPairs[0].first == differentPairs[1].second &&\n                 differentPairs[0].second == differentPairs[1].first) &&\n                (differentPairs[2].first == differentPairs[3].second &&\n                 differentPairs[2].second == differentPairs[3].first)) ||\n               ((differentPairs[0].first == differentPairs[2].second &&\n                 differentPairs[0].second == differentPairs[2].first) &&\n                (differentPairs[1].first == differentPairs[3].second &&\n                 differentPairs[1].second == differentPairs[3].first)) ||\n               ((differentPairs[0].first == differentPairs[3].second &&\n                 differentPairs[0].second == differentPairs[3].first) &&\n                (differentPairs[1].first == differentPairs[2].second &&\n                 differentPairs[1].second == differentPairs[2].first));\n    }\n\n    return false;\n}\n\nclass Solution {\npublic:\n    int countPairs(vector<int> &nums) {\n        const int n = nums.size();\n        vector<array<int, 8> > numsArray(n);\n\n        // Precompute array representation of nums\n        for (int i = 0; i < n; i++) {\n            int num = nums[i];\n            for (int j = 0; j < 8; j++) {\n                numsArray[i][7 - j] = num % 10;\n                num /= 10;\n            }\n        }\n\n        int ans = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                if (nums[i] == nums[j] || almost_equal(numsArray[i], numsArray[j])) {\n                    ans++;\n                }\n            }\n        }\n        return ans;\n    }\n};\n\n```
['C++']