Easy Code With Explanation Javascript and TypeScript
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve the problem of counting pairs (i, j) in an array where the numbers are "almost
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve the problem of counting pairs (i, j) in an array where the numbers are "almost equal," we need to identify pairs where one number can be converted to the other by swapping up to two pairs of digits. Given that this is a combinatorial problem, we will use string manipulations to generate all possible variations of each number by swapping digits.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Generate Variations:\n\n- For each number in the array, generate all possible numbers that can be formed by swapping two digits once and then by swapping two digits twice.\n- We use a set to store these variations to avoid duplicates.\n2. Count Matches:\n\n- Use a map to count occurrences of each number in its zero-padded string form.\n- For each number, look up how many times its variations have been seen before in the map.\n3. Update Map:\n\n- Update the map with the current number\u2019s zero-padded string form to count future occurrences.\n\n## Explanation of Each Function\n1. countPairs(nums: number[]): number:\n\n- Initialize a counter count and a map seen to track occurrences of each number.\n- For each number, generate all possible variations and update the count based on previously seen variations.\n- Update the map with the current number for future reference.\n2. generateVariations(num: number): Set<string>:\n\n- Convert the number to a zero-padded string.\n- Generate all possible variations by swapping each pair of digits once and twice.\n- Return a set of these variations to ensure uniqueness.\n\n3. swap(str: string, i: number, j: number): string:\n\n- Convert the string to an array, swap the characters at indices i and j, and convert it back to a string.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Generating variations involves swapping digits and can be $$O(d^2 )$$ where, $$d$$ is the number of digits in the largest number. Since we generate variations for up to two swaps, the complexity of generating variations can be $$O(d^4 )$$ in the worst case. If n is the number of elements in nums, the total time complexity can be $$O(n\u22C5d^4)$$ where d is the average number of digits in the numbers.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- The space complexity is primarily due to the space required for storing the variations and the map. If there are v unique variations, the space complexity is $$O(v)$$. The map stores occurrences of variations, which also contributes to the space complexity.\n\n# Code\n```typescript []\nfunction countPairs(nums: number[]): number {\n    let count = 0;\n    const seen = new Map<string, number>();\n\n    for (const num of nums) {\n        const variations = generateVariations(num);\n        for (const variation of variations) {\n            count += seen.get(variation) || 0;\n        }\n        const key = num.toString().padStart(7, \'0\');\n        seen.set(key, (seen.get(key) || 0) + 1);\n    }\n\n    return count;\n}\n\nfunction generateVariations(num: number): Set<string> {\n    const str = num.toString().padStart(7, \'0\');\n    const variations = new Set<string>([str]);\n\n    for (let i = 0; i < str.length; i++) {\n        for (let j = i + 1; j < str.length; j++) {\n            variations.add(swap(str, i, j));\n        }\n    }\n\n    for (let i = 0; i < str.length; i++) {\n        for (let j = i + 1; j < str.length; j++) {\n            const swapped = swap(str, i, j);\n            for (let k = 0; k < str.length; k++) {\n                for (let l = k + 1; l < str.length; l++) {\n                    variations.add(swap(swapped, k, l));\n                }\n            }\n        }\n    }\n\n    return variations;\n}\n\nfunction swap(str: string, i: number, j: number): string {\n    const arr = str.split(\'\');\n    [arr[i], arr[j]] = [arr[j], arr[i]];\n    return arr.join(\'\');\n}\n```\n\n## If you like the solution then please upvote........
['TypeScript']