2 Ways Detailed Description by C++ | Brute Force & Counting By Hash
Intuition\n Describe your first thoughts on how to solve this problem. \nTwo ways to slove the question.\n\n# Approach\n Describe your approach to solving the p
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTwo ways to slove the question.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Check one by one.\n    * Remove duplicates to get the smaller size of data first, any duplicates by n times we get n * (n-1) / 2 pairs by itself.\n    * In the non-duplicates set, any two numbers is almost equal, we can get mutiply the times of each showing pairs.\n    * For every two numbers, we have 6 different types : {0, 1, 2, 3, 4, 5+} different characters\n    * 0 => true\n    * 1 => false\n    * 2 => If two characters same => true\n    * 3 => if three characters same => true\n    * 4 => make 4 characters to 2 pairs, if any both pairs same => true\n    * 5+ => false\n2. Counting By Hash\n    * Sort the nums first by biggest to smallest first, so when we calculate any number we can get by swaping at most 2 times, we can get the smaller number (leading by 0)\n    * For each number, we counting duplicated times (c) it showing, and add c * (c-1) / 2 times to answer.\n    * For each number, we can get c * count[number] pairs by the previous numbers.\n    * Counting every number we can get by swaping at most 2 times, add them to the set.\n    * Each number showing in the set, we add c times to the count map\n# Complexity\n- Time complexity: O(N^2 * 8) -> O(N^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N) by remove duplicate and Counting map\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code 1\n```cpp []\nclass Solution {\npublic:\n    bool almost_equal(int i, int j){\n        if (i == j) return true;\n        int s1[8] = {0};\n        int s2[8] = {0};\n        int cnt = 0;\n        while(i || j){\n            s1[cnt] = i % 10;\n            s2[cnt] = j % 10;\n            cnt++;\n            i /= 10;\n            j /= 10;\n        }\n        int diff[4];\n        cnt = 0;\n        for (int i = 0; i < 8; i++){\n            if (s1[i] != s2[i]){\n                if (cnt == 4) return false;\n                diff[cnt++] = i;\n            }\n        }\n        if (cnt == 0) return true;\n        if (cnt == 1) return false;\n        int a = diff[0];\n        int b = diff[1];\n        if (cnt == 2) return (s1[diff[0]] == s2[diff[1]]) && (s1[diff[1]] == s2[diff[0]]);\n        int c = diff[2];\n        if (cnt == 3){\n            return (s1[a] == s2[b] && s1[b] == s2[c] && s1[c] == s2[a])|| \n                   (s1[a] == s2[c] && s1[b] == s2[a] && s1[c] == s2[b]);\n        }\n        int d = diff[3];\n        if (cnt == 4){\n            return (s1[a] == s2[b] && s1[b] == s2[a] && s1[c] == s2[d] && s1[d] == s2[c])||\n                   (s1[a] == s2[c] && s1[c] == s2[a] && s1[b] == s2[d] && s1[d] == s2[b])||\n                   (s1[a] == s2[d] && s1[d] == s2[a] && s1[b] == s2[c] && s1[c] == s2[b]);\n        }\n        return false;\n    }\n    int countPairs(vector<int>& nums) {\n        int ans = 0;\n        unordered_map<int,int> m;\n        vector<int> v;\n        for (int i : nums){\n            if (m[i] == 0) v.push_back(i);\n            m[i]++;\n        }\n        for (auto i : m) ans += i.second * (i.second - 1) / 2;\n        int n = v.size();\n        for (int i = 0; i < n; i++){\n            for (int j = i + 1; j < n; j++){\n                if (almost_equal(v[i], v[j])){\n                    ans += m[v[i]] * m[v[j]];\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n# Code 2\n```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n        int n = nums.size();\n        sort(nums.begin(), nums.end(), greater());\n        int ans = 0;\n        // for (auto i : nums) cout << i << " ";\n        // cout << endl;\n        unordered_map<int, int> count;\n        unordered_set<int> s;\n        for (int index = 0; index < n; index++){\n            int c = 1;\n            while (index + 1 < n && nums[index + 1] == nums[index]) c++, index++;\n            ans += c * (c - 1) / 2;\n            ans += count[nums[index]] * c;\n            string curr = to_string(nums[index]);\n            int k = curr.length();\n            for (int i = 0; i < k; i++){\n                for (int j = i; j < k; j++){\n                    swap(curr[i], curr[j]);\n                    for (int a = 0; a < k; a++){\n                        for (int b = a; b < k; b++){\n                            swap(curr[a], curr[b]);\n                            s.insert(stoi(curr));\n                            swap(curr[a], curr[b]);\n                        }\n                    }\n                    swap(curr[i], curr[j]);\n                }\n            }\n            for (int temp : s) count[temp] += c;\n            s.clear();\n        }\n        return ans;\n    }\n};\n```\n# \uD83D\uDD25 If you like the solution, Please Upvote \uD83D\uDD1D\n
['C++']