Java || 100% Beats
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nimport java.util.*;\n\npublic class Solution {\n    public int countPairs(int[] nums) {\n        Map<Integer, Integer> countMap = new HashMap<>();\n        for (int num : nums) {\n            countMap.put(num, countMap.getOrDefault(num, 0) + 1);\n        }\n\n        List<Integer> uniqueNums = new ArrayList<>(countMap.keySet());\n        Collections.sort(uniqueNums);\n\n        long ans = 0;\n        int n = uniqueNums.size();\n        for (int i = 0; i < n; ++i) {\n            for (int j = i + 1; j < n; ++j) {\n                String x = Integer.toString(uniqueNums.get(i));\n                String y = Integer.toString(uniqueNums.get(j));\n                if (helper(x, y)) {\n                    ans += (long) countMap.get(uniqueNums.get(j)) * countMap.get(uniqueNums.get(i));\n                }\n            }\n        }\n\n        for (int freq : countMap.values()) {\n            ans += (long) freq * (freq - 1) / 2;\n        }\n\n        return (int) ans;\n    }\n\n    private boolean helper(String x, String y) {\n        int count = 0;\n        List<Character> a = new ArrayList<>(), b = new ArrayList<>();\n        int l1 = x.length(), l2 = y.length();\n\n        for (int i = -1; i >= -Math.max(l1, l2) - 1; --i) {\n            char f = (i >= -l1) ? x.charAt(l1 + i) : \'0\';\n            char s = (i >= -l2) ? y.charAt(l2 + i) : \'0\';\n\n            if (f != s) {\n                a.add(f);\n                b.add(s);\n                count++;\n            }\n\n            if (count > 4) return false;\n        }\n\n        Map<Character, Integer> countA = new HashMap<>(), countB = new HashMap<>();\n        for (char c : a) countA.put(c, countA.getOrDefault(c, 0) + 1);\n        for (char c : b) countB.put(c, countB.getOrDefault(c, 0) + 1);\n\n        if (!countA.equals(countB)) return false;\n        if (count == 2 || count == 3) return true;\n        if (count == 4) {\n            char req = b.get(0);\n            for (int i = 1; i < 4; ++i) {\n                if (a.get(i) == req) {\n                    if (a.get(0) == b.get(i)) return true;\n                }\n            }\n        }\n        return false;\n    }\n}\n\n```
['Java']