CPP BEST SOLUTION
C++ SOLUTION\n\nclass Solution {\npublic:\n    string str(int digits,int num){\n        int a=log10(num)+1;\n        string s=to_string(num);\n        for(int i
**C++ SOLUTION**\n\nclass Solution {\npublic:\n    string str(int digits,int num){\n        int a=log10(num)+1;\n        string s=to_string(num);\n        for(int i=0;i<digits-a;i++){\n            s="0"+s;\n        }\n        return s;\n    }\n    unordered_set<string> cases(int digits,int num){\n        string s=str(digits,num);\n        int n=digits;\n        unordered_set<string>pos;\n        pos.insert(s);\n        for(int i=0;i<n;i++){\n            for(int j=i+1;j<n;j++){\n                swap(s[i],s[j]);\n                pos.insert(s);\n                for(int p=0;p<n;p++){\n                    for(int q=p+1;q<n;q++){\n                        swap(s[p],s[q]);\n                        pos.insert(s);\n                        swap(s[p],s[q]);\n                    }\n                }\n                swap(s[i],s[j]);\n            }\n        }\n        return pos;\n    }\n    int countPairs(vector<int>& nums) {\n        int n=nums.size();\n        int digits=to_string(*max_element(nums.begin(),nums.end())).size();\n        int cnt=0;\n        unordered_map<string,int>mpp;\n        for(int i=0;i<n;i++){\n            for(const auto& s:cases(digits,nums[i])){\n                cnt+=mpp[s];\n            }\n            mpp[str(digits,nums[i])]++;\n        }\n        return cnt;    \n    }\n};
['Greedy', 'C']