JAVA || Faster than 100% || Intuitive Approach || Extending the easir variant code ||
Intuition\n Describe your first thoughts on how to solve this problem. \nIntuition was keeping track of indexes where corresponding values in num1 and num2 has 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIntuition was keeping track of indexes where corresponding values in num1 and num2 has a mismatch.\nIn easier variant when only one swap was allowed, if we have exactly 2 pairs of such indexes, we can check whether they can be wapped to make a matach, example 127 and 172, here there are 2 mismatch, in num1 there is 2 while in num2 there is 7 this means we got a mismatch pair (2,7) , similarly next pair will be (7,2). now we have to check whethere we can do a swap do make them matach, and (a,b) ,(c,d) , after swap it will be (a,d) and (b, c) applying it here it would be (2,2) and(7,7). This approach works when atmost 1 swap\n\nIn this version, we can have atmost 2 swaps, \nMeaning either 0 or 1 or 2 swaps, \n1 swap case has been discussed, when there are 2 pairs of mismatch, \nfor 2 swaps, we either need 3 pairs or 4 pairs of mismatch. \nif there are more than 4 mismatch pairs, they cant be matched.\n\nIf we got 4 pairs, we must be able to group these pairs in group of 2 and 2 and check if they can be swapped to make same\n\nIf we got 3 pairs, checking the frequency of digits of pairs will do the job\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(1)\n\n\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int n = nums.length;\n        int count = 0;\n        \n        for (int k = 0; k < n; k++) {\n            for (int j = k + 1; j < n; j++) {\n                int x = nums[k];\n                int y = nums[j];\n                \n                if (x == y) {\n                    count++;\n                } else {\n                    String num1 = String.valueOf(x);\n                    String num2 = String.valueOf(y);\n                    \n                    \n                    if (num1.length() != num2.length()) {\n                        while (num1.length() < num2.length()) num1 = "0" + num1;\n                        while (num2.length() < num1.length()) num2 = "0" + num2;\n                    }\n                    \n                    int len = num1.length();\n                    int diff = 0;\n                    int[] a1 = new int[4];\n                    int[] a2  = new int[4];\n                    boolean possible=true;\n                    for (int p = 0; p < len; p++) {\n                        int c1 = num1.charAt(p) - \'0\';\n                        int c2 = num2.charAt(p) - \'0\';\n                        if (c1 != c2) {\n                            if(diff==4) {\n                                possible=false;\n                                break;\n                            }\n                            a1[diff] = c1;\n                            a2[diff] = c2;\n                            diff++;\n                        }\n                    }\n                    if(possible){\n                    if (diff == 2) {\n                        if (a1[0] == a2[1] && a1[1] == a2[0]) count++;\n                    } else if (diff == 3) {\n                        int []f=new int[10];\n                        for(int i=0;i<diff;i++){\n                            f[a1[i]]++;\n                            f[a2[i]]--;\n                        }\n                        boolean canbe=true;\n                        for(int i=0;i<10;i++) if(f[i]!=0) canbe=false;\n                        if(canbe) count++;\n                    }\n                    else if(diff==4){\n                        for(int i=1;i<4;i++){\n                            if(a1[0]==a2[i] && a1[i]==a2[0]){\n                                if(i==1 && a1[2]==a2[3] && a1[3]==a2[2]) {count++ ; break;}\n                                else if(i==2 && a1[1]==a2[3] && a1[3]==a2[1]) {count++ ; break;}\n                                else if(i==3 && a1[1]==a2[2] && a1[2]==a2[1]) {count++; break;}\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    return count;\n    }\n}\n\n```
['Java']