Easier to understand Binary search & unordered map use
Intuition\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 
# Intuition\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```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n\n        std::sort(nums.begin(), nums.end());\n        std::unordered_map<int, vector<int>> idx_map;\n        for(int n=0; n<nums.size(); n++)\n        {\n            auto nw = to_string(nums[n]);\n            if(!idx_map.count(nums[n]))\n            {\n                auto [itr, _] = idx_map.emplace(nums[n],std::vector<int>());\n                itr->second.push_back(n);\n            }\n            else\n            {\n                auto last_idx = idx_map[nums[n]].size() - 1;\n                if(idx_map[nums[n]][last_idx] != n)\n                {\n                    idx_map[nums[n]].push_back(n);\n                }\n            }\n            for(int i=0; i<nw.length()-1; i++)\n            {\n                for(int j=i+1; j<nw.length(); j++)\n                {\n                    if(nw[i] != nw[j])\n                    {\n                        string x(nw);\n                        x[i] = nw[j];\n                        x[j] = nw[i];\n                        auto nn = stoi(x);\n                        if(!idx_map.count(nn))\n                        {\n                            auto [itr, _] = idx_map.emplace(nn,std::vector<int>());\n                            itr->second.push_back(n);\n                        }\n                        else\n                        {\n                            auto last_idx = idx_map[nn].size() - 1;\n                            if(idx_map[nn][last_idx] != n)\n                            {\n                                idx_map[nn].push_back(n);\n                            }\n                        }\n                        for(int p=0; p<x.length()-1; p++)\n                        {\n                            for(int q=i+1; q<x.length(); q++)\n                            {\n                                if(x[p] != x[q])\n                                {\n                                    string y(x);\n                                    y[p] = x[q];\n                                    y[q] = x[p];\n                                    auto yy = stoi(y);\n                                    if(idx_map.find(yy) != idx_map.end())\n                                    {\n                                        auto last_idx = idx_map[yy].size() - 1;\n                                        if(idx_map[yy][last_idx] != n)\n                                        {\n                                            idx_map[yy].push_back(n);\n                                        }\n                                    }\n                                    else\n                                    {\n                                        auto [itr, _] = idx_map.emplace(yy,std::vector<int>());\n                                        itr->second.push_back(n);\n                                    }\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n        // for(const auto& [val, idcs]: idx_map)\n        // {\n        //     std::cout << val << "->";\n        //     for(const auto elm: idcs)\n        //     {\n        //         std::cout << elm << "|";\n        //     }\n        //     std::cout << "::: ";\n        // }\n        // std::cout << std::endl;\n        int count = 0;\n        for(int i=0; i<nums.size(); i++)\n        {\n            const auto& idcs = idx_map.find(nums[i])->second;\n            int l=0;\n            int r=idcs.size()-1;\n            int pos = r+1;\n            int find = i+1;\n            while(l <= r)\n            {\n                int m = (l+r)/2;\n                if(idcs[m] >= find)\n                {\n                    pos = m;\n                    r = m-1;\n                }\n                else\n                {\n                    l = m+1;\n                }\n            }\n            auto cnt = (idcs.size() - pos);\n            count = count + cnt;\n\n        }\n        return count;\n    }\n};\n```
['C++']