Easy Map Solution
Starting from beginning, use one and two swaps and then see if rearranged numbers are present or not.\nTo avoid overcounting just maintain of count of numbers o
Starting from beginning, use one and two swaps and then see if rearranged numbers are present or not.\nTo avoid overcounting just maintain of count of numbers of ending numbers only.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n        int n = nums.size();\n\n        int ans = 0;\n        map<int, int> mp; // numbers that are present\n        for(auto it: nums) mp[it]++;\n        for(int i = 0; i < n; i++){\n            mp[nums[i]]--; // decrease the count of current number as we are comparing next numbers.\n            ans += mp[nums[i]]; // using 0 operation\n            string s = to_string(nums[i]);\n            reverse(s.begin(), s.end());\n            while(s.size() < 7) s += \'0\';\n            set<int> vals; // all the numbers that can be made by rearranging\n\n            // one operation\n            for(int k = 0; k < 7; k++){\n                for(int j = k+1; j < 7; j++){\n                    if(s[k] == s[j]) continue;\n                    swap(s[k], s[j]);\n                    reverse(s.begin(), s.end());\n                    int cnt = stoi(s);\n                    reverse(s.begin(), s.end());\n                    if(mp.find(cnt) != mp.end()) vals.insert(cnt);\n                    swap(s[k], s[j]);\n                }\n            }\n\n            // two operation\n            for(int p = 0; p < 7; p++){\n                for(int j = p+1; j < 7; j++){\n                    if (s[p] == s[j]) continue;\n                    swap(s[p], s[j]);\n\n                    for (int k = 0; k < 7; k++) {\n                        for (int l = k + 1; l < 7; l++) {\n                            if (s[k] == s[l] || (k == p && l == j)) continue;\n                            swap(s[k], s[l]);  \n                            reverse(s.begin(), s.end());\n                            int cnt = stoi(s); \n                            reverse(s.begin(), s.end());\n                            if (mp.find(cnt) != mp.end()) vals.insert(cnt);\n                            swap(s[k], s[l]); \n                        }\n                    }\n\n                    swap(s[p], s[j]);\n                }\n            }\n            for(auto it: vals) ans += mp[it];\n        }\n\n        return ans;\n    }\n};\n```
['C++']