Sometimes brute force works.
Intuition\n Describe your first thoughts on how to solve this problem. \nCheck each pair with brute force. Not a fancy solution though.\n# Approach\n Describe y
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck each pair with brute force. Not a fancy solution though.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- Time complexity:$$O(n^2)$$\n\n- Space complexity:$$O(1)$$\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int res = 0;\n        int len = nums.length;\n        for (int i = 0; i < len; ++i) {\n            for (int j = i+1; j < len; ++j) {\n                if (good(nums[i], nums[j])) {\n                    ++res;\n                }\n            }\n        }\n        return res;\n    }\n\n    boolean good(int a, int b) {\n        if (a == b) return true;\n        int[] ofA = new int[4];\n        int[] ofB = new int[4];\n        int i = 0;\n        while (a > 0 || b > 0) {\n            int aLast = a % 10;\n            a /= 10;\n            int bLast = b % 10;\n            b /= 10;\n            if (aLast != bLast) {\n                if (i >=4) return false;\n                ofA[i] = aLast;\n                ofB[i] = bLast;\n                ++i;\n            }\n        }\n\n        return exchange(ofA, ofB, 0, 0) <= 2;\n    }\n\n    int exchange(int[] ofA, int[] ofB, int i, int cur) {\n        if (i == 4) return cur;\n        if (ofB[i] == ofA[i]) return exchange(ofA, ofB, i + 1, cur);\n        int min = 100;\n        for (int j = i + 1; j < 4; ++j) {\n            if (ofB[j] == ofA[i]) {\n                ofB[j] = ofB[i];\n                min = Math.min(exchange(ofA, ofB, i + 1, cur+1), min);\n                if (min <= 2) return min;                \n                ofB[j] = ofA[i];\n            }\n        }\n        return min;\n    }\n}\n\n```
['Java']