Beats 100% users || Java || Very Easy Code
Intuition\nThe problem is to determine how many pairs of numbers in the array can become equal by performing at most two digit swaps on one of the numbers in ea
# Intuition\nThe problem is to determine how many pairs of numbers in the array can become equal by performing at most two digit swaps on one of the numbers in each pair. To solve this, we need to:\n\nIdentify Mismatches: Determine the positions where the digits of two numbers differ.\nCheck Swapping Possibilities: Depending on the number of mismatches (0, 2, 3, or 4), check if swapping digits can make the two numbers equal.\n\n# Approach\nCount Pairs:\n\nIterate over each pair of numbers in the nums array.\nFor each pair, use the eql function to determine if they can be made equal with at most two swaps.\nMismatch Identification (eql function):\n\nConvert each number to an array of its digits.\nCompare the digits of the two numbers to find mismatched positions.\nDepending on the number of mismatches:\n0 Mismatches: The numbers are already equal.\n2 Mismatches: Check if a single swap can resolve the differences.\n3 Mismatches: Check if two swaps can resolve the differences by considering all possible swaps.\n4 Mismatches: Check all possible ways to resolve the mismatches with two swaps.\nReturn true if the mismatches can be resolved; otherwise, return false.\nDigit Array Conversion (arr function):\n\nConvert a number into an array of its digits, padding with zeros if needed to ensure an 8-digit representation.\n\n# Complexity\n- Time complexity:\nO(n2)\n\n- Space complexity:\nO(1)\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int n = nums.length;\n        int cnt = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                if (eql(nums[i], nums[j])) {\n                    \n                    cnt++;\n                }\n            }\n        }\n        return cnt;\n    }\n\n    public static boolean eql(int x, int y) {\n        int[] a1 = arr(x);\n        int[] a2 = arr(y);\n        int[] mis = new int[4];\n        int cnt = 0;\n        for (int k = 0; k < 8; k++) {\n            if (a1[k] != a2[k]) {\n                if (cnt == 4)\n                    return false;\n                mis[cnt] = k;\n                cnt++;\n            }\n        }\n        if (cnt == 0)\n            return true;\n        else if (cnt == 2) {\n            return a1[mis[0]] == a2[mis[1]] && a1[mis[1]] == a2[mis[0]];\n            \n        } else if(cnt==3){\n            int a=mis[0];\n            int b=mis[1];\n            int c=mis[2];\n            return (a1[a]==a2[b] && a1[b]==a2[c]&& a1[c]==a2[a])||(a1[a]==a2[c] && a1[c]==a2[b]&& a1[b]==a2[a]);\n        }\n        else if (cnt == 4) {\n            int a=mis[0];\n            int b=mis[1];\n            int c=mis[2];\n            int d=mis[3];\n            return(a1[a]==a2[b]&& a1[b]==a2[a] && a1[c]==a2[d]&& a1[d]==a2[c])||\n                  (a1[a]==a2[c]&& a1[c]==a2[a] && a1[b]==a2[d]&& a1[d]==a2[b])||\n                  (a1[a]==a2[d]&& a1[d]==a2[a] && a1[b]==a2[c]&& a1[c]==a2[b]);\n        } else {\n            return false;\n        }\n    }\n\n    public static int[] arr(int num) {\n        int[] cnt = new int[8];\n        int i = 0;\n        while (num > 0) {\n            cnt[i] = num % 10;\n            num /= 10;\n            i++;\n        }\n        return cnt;\n    }\n}\n```
['Java']