Easy Java solution, no fancy things
Intuition\n Describe your first thoughts on how to solve this problem. \nGenerate all numbers which could be generated using swapping 0, 1 or 2 times and add th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGenerate all numbers which could be generated using swapping 0, 1 or 2 times and add them to hashmap with freq. For the current number, check how many times it previously was seen and add it to answer.\n\n# Code\n```java []\nclass Solution {\n\n    int getNum(char c[]) {\n        int num = 0;\n        int n = c.length;\n        int prod = 1;\n        for (int i = n - 1; i >= 0; i--) {\n            num = num + (c[i] - \'0\') * prod;\n            prod = prod * 10;\n        }\n\n        return num;\n    }\n\n    void swap(char c[], int x, int y) {\n        char temp = c[x];\n        c[x] = c[y];\n        c[y] = temp;\n    }\n\n    void generate(char c[], int left, HashSet<Integer> set) {\n        int n = c.length;\n\n        int num = getNum(c);\n\n        set.add(num);\n\n        if (left == 0) {\n            return;\n        }\n\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                swap(c, i, j);\n\n                generate(c, left - 1, set);\n\n                swap(c, i, j);\n            }\n        }\n    }\n\n    public int countPairs(int[] nums) {\n        int ans = 0;\n\n        int n = nums.length;\n\n        HashMap<Integer, Integer> map = new HashMap<>();\n\n        for (int i = 0; i < n; i++) {\n            int cur = nums[i];\n            ans = ans + map.getOrDefault(cur, 0);\n\n            HashSet<Integer> swapped = new HashSet<>();\n\n            // append 0 to start to handle case like 1, 10, 100\n            String curVal = String.valueOf(cur);\n            while (curVal.length() < 8) {\n                curVal = "0" + curVal;\n            }\n\n            generate(curVal.toCharArray(), 2, swapped);\n\n            for (int swap : swapped) {\n                map.put(swap, map.getOrDefault(swap, 0) + 1);\n            }\n        }\n\n        return ans;\n    }\n}\n```
['Java']