Java Brute Force, using map to group all the ones with same digits
Intuition\n Describe your first thoughts on how to solve this problem. \nSince all the numbers are less than 10^7, so make all the number string with length = 7
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince all the numbers are less than 10^7, so make all the number string with length = 7, attach 0 in front to make them same length.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing a hashmap, with key = sorted string, so if two string with same chars will have same key. Only compare the ones with same key.\nWhen do comparision, check the indexes with different chars. If their are more than 4 diffs, then it is impossible to make it. If the diff = 2 or 3, we can make it in 1 or 2 swithes. (In fact, there is not possible to have 1 diff only if 2 strings with same chars.) If there are 4 diffs, then make sure it can switch 0,1 or 0,2 or 0,3. Since 0123 and 1230 can\'t make it 2 switches.\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int n = nums.length;\n        String[] strs = new String[n];\n\t\t\n\t\tint result = 0;\n        Map<String, List<Integer>> map = new HashMap();\n        for (int i = 0; i < n; i++) {\n        \tint now = nums[i];\n        \tString str = now + "";\n        \twhile (str.length() < 7)\n        \t\tstr = "0" + str;\n        \tstrs[i] = str;\n        \tchar[] chars = str.toCharArray();\n        \tArrays.sort(chars);\n        \tString key = new String(chars);\n        \tif (!map.containsKey(key))\n        \t\tmap.put(key, new ArrayList());\n        \tmap.get(key).add(i);\n        }\n        for (List<Integer> list : map.values()) {\n\t        for (int i = 1; i < list.size(); i++) {\n\t        \tfor (int j = 0; j < i; j++) {\n\t        \t\tif (check(strs[list.get(i)], strs[list.get(j)]))\n\t        \t\t\tresult++;\n\t        \t}\n\t        }\n        }\n        return result;\n    }\n\t\n\tprivate static boolean check(String a, String b) {\n\t\tList<Integer> list = new ArrayList();\n\t\tfor (int i = 0; i < a.length(); i++) {\n\t\t\tif (a.charAt(i) != b.charAt(i))\n\t\t\t\tlist.add(i);\n\t\t}\n\t\tif (list.size() == 1 || list.size() > 4)\n\t\t\treturn false;\n\t\tif (list.size() < 4)\n\t\t\treturn true;\n\t\tif (check(a, b, list, 0, 1))\n\t\t\treturn true;\n\t\tif (check(a, b, list, 0, 2))\n\t\t\treturn true;\n\t\treturn (check(a, b, list, 0, 3));\n    }\n\t\n\tprivate static boolean check(String a, String b, List<Integer> list, int x, int y) {\n\t\treturn a.charAt(list.get(x)) == b.charAt(list.get(y)) && a.charAt(list.get(y)) == b.charAt(list.get(x));\n\t}\n}\n```
['Java']