Intuitive solution though C++
Intuition\nCalculate every value for every element in nums after swap at most twice, after that, you can get the number of element with same value within time c
# Intuition\nCalculate every value for every element in nums after swap at most twice, after that, you can get the number of element with same value within time complexity O(logN) using [multiset](https://en.cppreference.com/w/cpp/container/multiset)\n\n# Approach\nThe solution is intuitive. But several tips are worth to mentained to help you get AC\n1. Use int type to swap for every element to avoid TLE. if you tranfer that to string before swap and tranfer back to int, you will get TLE. I think that is because the memory capacity of int is equal with a char in string, the process of number in string format will increase extra momery and time consumption.\n2. Multiset can help to you get the amount with same value after swap position within time complexity of O(logN)\n3. Count for both (i, j) and (j, i) will help you to process the case of [2, 20], that can save lots of code and time for you.\n\n# Complexity\n- Time complexity:$O(C(7, 2) * C(7, 2) * N * logN)$ N=nums.length\n\n- Space complexity:$O(N)$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n        multiset<int> appear_times;\n        for(int i = 0; i < nums.size(); ++i) appear_times.insert(nums[i]);\n        int result = 0;\n        set<int> change_set;\n        int base10_x = 1, base10_y = 1, base10_nx = 1, base10_ny = 1;\n        for(int i = 0; i < nums.size(); ++i) {\n         base10_x = 1;\n            string s = to_string(nums[i]);\n            for (int x = 0; x < s.size(); ++x) {\n             base10_x *= 10;\n                base10_y = base10_x;\n                for (int y = x + 1; y < s.size(); ++y) {\n                    base10_y *= 10;\n                    // calculate the value after swap postion thought `int` type, it will be TLE if if you use int -> string -> swap -> int \n                    int x_pos_val = nums[i] % base10_x / (base10_x / 10);\n                    int y_pos_val = nums[i] % base10_y / (base10_y / 10);\n                    int change_val = nums[i] + x_pos_val * (base10_y / 10 - base10_x / 10) + y_pos_val *  (base10_x / 10 - base10_y / 10);\n                    base10_nx = base10_x / 10;\n                    for (int nx = x; nx < s.size(); ++nx) {\n                        base10_nx *= 10;\n                        base10_ny = base10_nx;\n                        for (int ny = nx + 1; ny < s.size(); ++ny) {\n                            base10_ny *= 10;\n                            int nx_pos_val = change_val % base10_nx / (base10_nx / 10);\n                            int ny_pos_val = change_val % base10_ny / (base10_ny / 10);\n                            int change_val2 = change_val + nx_pos_val * (base10_ny / 10 - base10_nx / 10) + ny_pos_val * (base10_nx / 10 - base10_ny / 10);\n                            if(change_set.count(change_val2) == 0) {\n                                change_set.insert(change_val2);\n                                int increase_num = appear_times.count(change_val2);\n                                if(change_val2 == nums[i]) --increase_num;\n                                result += increase_num;\n                                if(((y == s.size() - 1 && x_pos_val == 0) || (ny == s.size() - 1 && nx_pos_val == 0))\n                                    && !(x == nx && y == ny)) result += increase_num;\n                            }\n                        }\n                    }\n                    int change_val2 = change_val;\n                    if(change_set.count(change_val2) == 0) {\n                        change_set.insert(change_val2);\n                        int increase_num = appear_times.count(change_val2);\n                        if(change_val2 == nums[i]) --increase_num;\n                        result += increase_num;\n                        if(y == s.size() - 1 && x_pos_val == 0) result += increase_num;\n                    }\n                }\n            }\n            change_set.clear();\n            if (s.size() == 1) result += appear_times.count(nums[i]) - 1;\n        }\n        return result / 2;\n    }\n};\n```\nIf this is unclear, let me know and I can make a video for you.\n\nIf this is helpful for you, upvote will be grateful.
['C++']