[Java] Enumerating all the possible neighbors
Intuition\nAfter trying so many optimizations for the brute force approach, which tries to enumerate all the results after swapping nums[i] and nums[j], where i
# Intuition\nAfter trying so many optimizations for the brute force approach, which tries to enumerate all the results after swapping nums[i] and nums[j], where i < j and see if it matches takes $$O(N^2.D^4)$$, where D = number of digits in a nums[x]. There are multipe approaches which optimizes this approach as well.\n\nHere, the idea is to store the all the enumerated results for each index (i) (which I am also calling as neighbors in my code) and check if they exist in the nums array for all j > i. This can be easily achieved by maintaing a hash map having frequencies of each number.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* Step 1. Build a frequency map.\n* Step 2. For each num, generate all the neighbors.\n* Step 3. Check if they exist in the frequency map.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(N.D^4)$$\n\n- Space complexity:\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        int count = 0;\n        Map<Integer, Integer> freqMap = new HashMap<>();\n        for (int num: nums) {\n            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);\n        }\n\n        for (int num: nums) {\n            int currNumFreq = freqMap.get(num);\n            if (currNumFreq == 1) {\n                freqMap.remove(num);\n            } else {\n                freqMap.put(num, currNumFreq - 1);\n            }\n\n            Set<Integer> neighbors = generateNeighbors(num);\n            // System.out.println("num: " + num);\n            // System.out.println("neighbors: " + neighbors);\n            for (int neighbor: neighbors) {\n                count += freqMap.getOrDefault(neighbor, 0);\n            }\n        }\n\n        return count;\n    }\n\n    private Set<Integer> generateNeighbors(int num) {\n        Set<Integer> set = new HashSet<>();\n        set.add(num);\n        String numStr = String.valueOf(num);\n        int zeros = Math.min(7 - numStr.length(), 4);\n        StringBuilder sb = new StringBuilder();\n        for (int i = 0 ; i < zeros ; i++) {\n            sb.append(\'0\');\n        }\n\n        sb.append(numStr);\n        char numArr[] = sb.toString().toCharArray();\n        int N = numArr.length;\n\n        for (int i = 0 ; i < N ; i++) {\n            for (int j = i + 1 ; j < N ; j++) {\n                char temp = numArr[i];\n                numArr[i] = numArr[j];\n                numArr[j] = temp;\n\n                // System.out.println(Integer.parseInt(new String(first)));\n                int x = Integer.parseInt(new String(numArr));\n                set.add(x);\n\n                for (int k = 0 ; k < N ; k++) {\n                    for (int l = k + 1 ; l < N ; l++) {\n                        char temp1 = numArr[k];\n                        numArr[k] = numArr[l];\n                        numArr[l] = temp1;\n\n                        // System.out.println(Integer.parseInt(new String(first)));\n                        x = Integer.parseInt(new String(numArr));\n                        set.add(x);\n\n                        temp1 = numArr[k];\n                        numArr[k] = numArr[l];\n                        numArr[l] = temp1;\n                    }\n                }\n\n                temp = numArr[i];\n                numArr[i] = numArr[j];\n                numArr[j] = temp;\n            }\n        }\n\n        return set;\n    }\n}\n```
['Java']