🔥🔥🔥C++ | Step-Wise Approach Explained 🔥🔥🔥 | Counting Pairs with Two Swaps
Approach\n\n### 1. Convert Numbers to Fixed-Length Strings\nTo compare numbers of different lengths, we convert each number into a fixed-length string by prepen
## Approach\n\n### 1. Convert Numbers to Fixed-Length Strings\nTo compare numbers of different lengths, we convert each number into a fixed-length string by prepending zeros. The length of the strings is determined by the number of digits in the largest number in the array.\n\n### 2. Generate All Possible Formations\nFor each number, we generate all possible variations of the number that can be formed by swapping any two digits at most twice. This is done using nested loops to explore all possible digit swaps. These variations are stored in a set to avoid duplicates.\n\n### 3. Count Matching Pairs\nWe maintain a hashmap to store the count of each variation we encounter as we iterate through the array. For each number, we check how many of its variations have already been encountered, and we add this count to our result.\n\n### 4. Complexity\n- The maximum length of the numbers is 7 digits, so the complexity of generating all formations is `7*7*7*7`, which is manageable.\n- The overall time complexity depends on the number of numbers in the input array and the number of digits in the largest number.\n\n## Code\n\n```cpp\nclass Solution {\npublic:\n    // Convert a number to a fixed-length string by prepending zeros.\n    string generateString(int num, int digit) {\n        string s = to_string(num);\n        while (s.length() < digit) {\n            s = "0" + s;\n        }\n        return s;\n    }\n\n    // Generate all possible variations of a number by swapping digits.\n    void generateFormations(set<string>& s, int num, int digit) {\n        string nums = generateString(num, digit);\n        s.insert(nums);\n        for (int i = 0; i < nums.length() - 1; i++) {\n            for (int j = i + 1; j < nums.length(); j++) {\n                if (nums[i] == nums[j]) continue;\n                swap(nums[i], nums[j]);\n                s.insert(nums);\n                for (int k = 0; k < nums.length() - 1; k++) {\n                    for (int l = k + 1; l < nums.length(); l++) {\n                        if (nums[k] == nums[l]) continue;\n                        swap(nums[k], nums[l]);\n                        s.insert(nums);\n                        swap(nums[k], nums[l]);\n                    }\n                }\n                swap(nums[i], nums[j]);\n            }\n        }\n    }\n\n    // Count pairs of numbers that can be made equal by two swaps.\n    int countPairs(vector<int>& nums) {\n        int n = nums.size();\n        int digit = to_string(*max_element(nums.begin(), nums.end())).size();\n        unordered_map<string, int> mp;\n        int ans = 0;\n\n        for (int i = 0; i < nums.size(); i++) {\n            unordered_set<string> s;\n            generateFormations(s, nums[i], digit);\n            for (auto u : s) {\n                if (mp[u]) {\n                    ans += mp[u];\n                }\n            }\n            mp[generateString(nums[i], digit)] += 1;\n        }\n        return ans;\n    }\n};\n
['C++']