Easy Video Solution (Critical Thinking )🔥 || How to 🤔 in Interview || Simulation || String || JAVA
Intuition\n# If you like the solution Please Upvote and subscribe to my youtube channel\n\n\n\n\n# EASY VIDEO EXPLANATION \n\n\nhttps://youtu.be/weYmUnmqwZ4\n\n
# Intuition\n# **If you like the solution Please Upvote and subscribe to my youtube channel**\n\n\n\n\n# EASY VIDEO EXPLANATION \n\n\nhttps://youtu.be/weYmUnmqwZ4\n\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 []\n/*\n\nscenarios :: Possible no of differ digits::\n\n\ncase 1.) if 0 digits differ then already equal\n\n\n\ncase 2.) if 1 digits differ then can\'t made equal by given operation\n    exp :: 123 & 124\n\n\n\ncase 3.) if 2 digits differ then we can made 1 swap to make equal\n    exp :: (2310,2130) => arrays => \n            ar1 : [0,1,3,2] \n            ar2 : [0,3,1,2]\n\n            differ ind = [1,2] \n            \n            1->[2]\n            2->[1]\n\n            0132  -> (1,2) -> 0312\n \n\n\n        (ar1[1]==ar2[2] && ar1[2]==ar2[1])\n\n\n\ncase 4.) if 3 digits differ then we can made 2 swap to make equal\n    exp ::  (2310,0213)  => arrays =>\n            ar1 : [0,1,3,2] \n            ar2 : [3,1,2,0]\n\n            differ ind : [0,2,3]\n\n            0 -> [2,3]\n            2 -> [0,3]\n            3 -> [0,2]\n\n            0132 -> (0,2) -> 3102 -> (2,3) -> 3120\n            0132 -> (0,3) -> 2130 -> (3,2) -> 2103\n\n\n\n        (ar1[0]==ar2[3] && ar1[3]==ar2[2] && ar1[2]==ar2[0])\n        (ar1[0]==ar2[2] && ar1[2]==ar2[3] && ar1[3]==ar2[0])\n\n\n\ncase 5.) if 4 digits differ then we can made 2 swap to make equal\n    exp :: (1023,2310) => arrays =>\n            ar1 : [3,2,0,1]\n            ar2 : [0,1,3,2]\n\n            differ ind : [0,1,2,3]\n\n            0 -> [1,2,3]\n            1 -> [0,2,3]\n            2 -> [0,1,3]\n            3 -> [0,1,2]\n\n            1023 -> (0,1) -> 0123 -> (2,3) -> 0132\n            1023 -> (0,2) -> 2013 -> (1,3) -> 2103\n            1023 -> (0,3) -> 3021 -> (1,2) -> 3201\n\n\n        (ar1[0]==ar2[1] && ar1[1]==ar2[0] && ar1[2]==ar2[3] && ar1[3]==ar2[2])\n        (ar1[0]==ar2[2] && ar1[2]==ar2[0] && ar1[1]==ar2[3] && ar1[3]==ar2[1])\n        (ar1[0]==ar2[3] && ar1[3]==ar2[0] && ar1[2]==ar2[1] && ar1[1]==ar2[2])  \n\n\n\ncase 6.) if more the 4 digits differ we can\'t do in 2 swaps\n\n*/\n\nclass Solution {\n\n    private int[] convertNumtoArr(int num){\n        int[] tmp = new int[8];\n        int i=0;\n        while(num>0){\n            tmp[i]=num%10;\n            num /=10;\n            i++;\n        }\n\n        return tmp;\n    }\n\n    private boolean isEqual(int x,int y){\n        int[] xAr = convertNumtoArr(x);\n        int[] yAr = convertNumtoArr(y);\n\n        int diffDigitCnt=0;\n        int[] diffDigitInd=new int[4];\n        for(int i=0;i<8;i++){\n            if(xAr[i]!=yAr[i]){\n                if(diffDigitCnt==4){\n                    return false;\n                }\n                diffDigitInd[diffDigitCnt]=i;\n                diffDigitCnt++;\n            }\n        }\n\n        if(diffDigitCnt==0){\n            return true;\n        }\n\n        if(diffDigitCnt==2){\n            int a = diffDigitInd[0];\n            int b = diffDigitInd[1];\n\n            return (xAr[a]==yAr[b] && xAr[b]==yAr[a]);\n        }\n\n        if(diffDigitCnt==3){\n            int a = diffDigitInd[0];\n            int b = diffDigitInd[1];\n            int c = diffDigitInd[2];\n\n            return (xAr[a]==yAr[b] && xAr[b]==yAr[c] && xAr[c]==yAr[a])\n                ||(xAr[a]==yAr[c] && xAr[c]==yAr[b] && xAr[b]==yAr[a]);\n        }\n\n        if(diffDigitCnt==4){\n            int a = diffDigitInd[0];\n            int b = diffDigitInd[1];\n            int c = diffDigitInd[2];\n            int d = diffDigitInd[3];\n\n            return (xAr[a]==yAr[b] && xAr[b]==yAr[a] && xAr[c]==yAr[d] && xAr[d]==yAr[c])||\n            (xAr[a]==yAr[c] && xAr[c]==yAr[a] && xAr[b]==yAr[d] && xAr[d]==yAr[b])|| \n            (xAr[a]==yAr[d] && xAr[d]==yAr[a] && xAr[c]==yAr[b] && xAr[b]==yAr[c]);\n        }\n\n        return false;\n    }\n\n\n    public int countPairs(int[] nums) {\n        int len = nums.length;\n        int res = 0;\n        for(int i=0;i<nums.length-1;i++){\n            for(int j=i+1;j<nums.length;j++){\n                if(isEqual(nums[i],nums[j])){\n                    res++;\n                }\n            }\n        }\n\n        return res;\n    }\n}\n```
['Math', 'String', 'Java']