Easy Java Solution
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 []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int n = nums.length;\n        int result = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = i+1; j < n; j++) {\n                result += almostEqual(nums[i], nums[j]) ? 1 : 0;\n            }\n        }\n        return result;\n    }\n\n    private boolean almostEqual(int a, int b) {\n        int found = 0;\n        // first 4 different digits for a and b\n        int[] ad = new int[4];\n        int[] bd = new int[4];\n        while (a > 0 || b > 0) {\n            if (a%10 != b%10) {\n                ad[found] = a%10;\n                bd[found] = b%10;\n                found++;\n            }\n            a /= 10;\n            b /= 10;\n            if (found == 4) {\n                break;\n            } \n        }\n\n        if (found <= 1) {\n            return found == 0;\n        }\n        if (found == 2 || found == 3) {\n            Arrays.sort(ad);\n            Arrays.sort(bd);\n            return a == b && Arrays.equals(ad, bd);\n        }\n        // found == 4\n        // need to get a matching pair first with the same indices,\n        // then all digits should match in any order\n        for (int i = 0; i < 3; i++) {\n            for (int j = i+1; j < 4; j++) {\n                if (ad[i] == bd[j] && ad[j] == bd[i]) {\n                    Arrays.sort(ad);\n                    Arrays.sort(bd);\n                    // a == b -> check if the rest digits are equal, found 5+ case\n                    return a == b && Arrays.equals(ad, bd);\n                }\n            }\n        }\n        return false;\n    }\n}\n```
['Java']