Easy Hash map solution (search all the formations)
Intuition\nAs the maximum digits which can be there are 7 as (nums[i] < 10^7)\nthe count of maximum numbers we can form after two swaps are C(n,2)  \times C(n,2
# Intuition\nAs the maximum digits which can be there are $$7$$ as ($$nums[i] < 10^7$$)\nthe count of maximum numbers we can form after two swaps are $$C(n,2)  \\times C(n,2)$$\nwhich will always be less than $$(21 \\times 21)$$\n\nso we can search if the formed number can be found in the array or not\n\n# Approach\n\ncreate a hashmap\ncreate a value result = 0(which will storing result)\n\ntraverse the array from 0 to n-1\n- find all the number which can be formed using max 4 swaps \n- search the found numbers in the hashmap and add the count to the result\n- add the current number to hashmap\n\nNote  :- Sort the array in increasing or decreasing order according to the direction of traversal\nbecause 1000 can form 100 but 100 can\'t form 1000\nso if you are traverssing left to right sort in increasing ordere\nand of you are traversing in right to left order sort in decreasing order \n\n\n# Complexity\n- Time complexity:\n $$O(n*21*21)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    void swap(char &a,char &b)\n    {\n        char t = a;\n        a = b;\n        b = t;\n    }\n    int countPairs(vector<int>& nums) {\n        int n = nums.size();\n        vector<string>numstring;\n        sort(nums.begin(),nums.end()); // sorting in increasing order\n        for(int i = 0;i<n;i++)\n        {\n            string str = to_string(nums[i]);\n            numstring.push_back(str);\n        }  \n\n        unordered_map<int,int>hash; // declaring the hashmap\n        int count = 0;\n        for(int i = 0;i<n;i++)\n        {\n/* unordered_map seen Hash map is to check \nif we are creating some number twice by swapping operations*/\n            unordered_map<int,int>seen; \n            int currcand = stoi(numstring[i]);\n            string str = (numstring[i]);\n            count = count + hash[currcand];\n            seen[currcand]++;\n            //forming the numbers from str\n            for(int index1 = 0;index1<str.size();index1++) \n            {\n                for(int index2 = index1+1;index2<str.size();index2++)\n                {\n                    swap(str[index1],str[index2]);\n                    int cand1 = stoi(str);\n                    if(seen[cand1] == 0)\n                    {\n                        count = count + hash[cand1];\n                        seen[cand1]++;\n                    }\n\n                    for(int index3 = 0;index3 < str.size();index3++)\n                    {\n                        for(int index4 = index3+1;index4 < str.size();index4++)\n                        {\n                            if((index1 == index3) && (index2 == index4))\n                                continue;\n                            if((index1 == index4) && (index2 == index3))\n                                continue;\n                            \n                            swap(str[index3],str[index4]);\n                            int cand2 = stoi(str);\n                            if(seen[cand2] == 0)\n                            {   \n                                count = count + hash[cand2];\n                                seen[cand2]++;\n                            }\n\n                            swap(str[index3],str[index4]);\n                        }\n                    }\n                    swap(str[index1],str[index2]);\n                }\n            }\n            hash[currcand] ++;\n        }\n        return count;\n    }\n};\n```\n\n\n# Comment if you have any doubt\n\n
['Hash Table', 'Sorting', 'C++']