Simple Approach Explained!!✅💯🔥
\n# Complexity\n- Time complexity: O(n * maxSize^4)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n * maxSize^2)\n Add your space complex
\n# Complexity\n- Time complexity: O(n * maxSize^4)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n * maxSize^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    // Variable to store the maximum number of digits among all numbers in nums\n    int maxSize = 0;\n\n    // Function to convert a number to a string of length maxSize by padding\n    // with zeros\n    string makeDigit(int num) {\n        string s = to_string(num);\n        int n = s.size();\n        while (n < maxSize) { // While the length is less than maxSize\n            s = "0" + s;      // Pad with leading zeros\n            n++;\n        }\n        return s;\n    }\n\n    // Function to generate all unique strings by swapping any two digits any\n    // number of times\n    unordered_set<string> makeAllSwaps(int num) {\n        string s = makeDigit(num);\n        unordered_set<string> res;\n        res.insert(s);\n\n        // Loop to generate all permutations by swapping\n        for (int i = 0; i < maxSize; i++) {\n            for (int j = i + 1; j < maxSize; j++) {\n                swap(s[i], s[j]); // Swap two digits\n                res.insert(s);    // Insert the new permutation\n\n                // Further swap other digits in the new permutation to generate\n                // more unique permutations\n                for (int l = 0; l < maxSize; l++) {\n                    for (int k = l + 1; k < maxSize; k++) {\n                        if (s[l] != s[k])\n                            swap(s[l], s[k]);\n                        res.insert(s);\n                        swap(s[l], s[k]);\n                    }\n                }\n                swap(s[i], s[j]);\n            }\n        }\n\n        return res;\n    }\n\n    int countPairs(vector<int>& nums) {\n        int n = nums.size();\n        maxSize = to_string(*max_element(nums.begin(), nums.end())).size();\n        int ans = 0;\n        unordered_map<string, int> mp;\n\n        for (int i = 0; i < n; i++) {\n            // Generate all unique permutations for the current number\n            for (auto& s : makeAllSwaps(nums[i])) {\n                // If this permutation is already in the map, add its count to\n                // ans\n                if (mp.count(s)) {\n                    ans += mp[s];\n                }\n            }\n            // Store the zero-padded representation of the current number in the\n            // map\n            mp[makeDigit(nums[i])]++;\n        }\n\n        return ans;\n    }\n};\n```
['C++']