Brute Force || Beats 92% || Easy to understand || Maths || C++
Complexity\n- Time complexity:O(n * 8^4)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(max(nums))\n Add your space complexity here, e.g. O
# Complexity\n- Time complexity:$$O(n * 8^4)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(max(nums))$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n        int maxi = *max_element(nums.begin(), nums.end());\n        vector<int> freq(maxi + 1), vis(maxi + 1, -1);\n        int n = nums.size(), ans = 0;\n        for(int i=0; i<n; i++){\n            int num = nums[i];\n            int an = 10000000;\n            for(int a=0; a<8; a++){\n                int bn = 10000000;\n                for(int b=0; b<8; b++){\n                    // swap the ath digit and bth digit\n                    int da = num / an;\n                    int diga = da % 10;\n                    int db = num / bn;\n                    int digb = db % 10;\n                    int new_num = num - diga * an + digb * an - digb * bn + diga * bn;\n                    int cn = 10000000;\n                    for(int c=0; c<8; c++){\n                        int dn = 10000000;\n                        for(int d=0; d<8; d++){\n                            // swap the cth digit and dth digit\n                            int dc = new_num / cn;\n                            int digc = dc % 10;\n                            int dd = new_num / dn;\n                            int digd = dd % 10;\n                            int nn = new_num - digc * cn + digd * cn - digd * dn + digc * dn;\n                            if(nn <= maxi && vis[nn] < i){\n                                vis[nn] = i;\n                                ans += freq[nn];\n                            }\n                            dn /= 10;\n                        }\n                        cn /= 10;\n                    }\n                    bn /= 10;\n                }\n                an /= 10;\n            }\n            freq[nums[i]]++;\n        }\n        return ans;\n    }\n};\n```
['C++']