Fastest Solution in C++, Java, and Python
Credit:\nSpecial thanks to bilyhurington for his c++ solution which derives the other forms.\n\n# Code\nc++ []\nclass Solution {\npublic:\n    const int L=7;\n 
# Credit:\nSpecial thanks to [bilyhurington](https://leetcode.com/u/bilyhurington) for his c++ solution which derives the other forms.\n\n# Code\n```c++ []\nclass Solution {\npublic:\n    const int L=7;\n    int countPairs(vector<int>& nums) {\n        int n=nums.size();\n        unordered_map<string,int> mp,id;\n        int ans=0;\n        for(int i=1;i<=n;i++){\n            int x=nums[i-1];\n            string s;\n            for(int j=0;j<L;j++){\n                s+=(x%10)+\'0\';\n                x/=10;\n            }\n            ans+=mp[s];\n            mp[s]++;\n            id[s]=i;\n            for(int p=0;p<L;p++){\n                for(int q=p+1;q<L;q++){\n                    if(s[p]==s[q]) continue;\n                    swap(s[p],s[q]);\n                    if(id[s]<i){\n                        id[s]=i;\n                        mp[s]++;\n                    }\n                    for(int r=p;r<L;r++){\n                        for(int t=r+1;t<L;t++){\n                            if(s[r]==s[t]) continue;\n                            swap(s[r],s[t]);\n                            if(id[s]<i){\n                                id[s]=i;\n                                mp[s]++;\n                            }\n                            swap(s[r],s[t]);\n                        }\n                    }\n                    swap(s[p],s[q]);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n```java []\nclass Solution {\n    private static final int L = 7;\n\n    public int countPairs(int[] nums) {\n        int n = nums.length;\n        Map<String, Integer> mp = new HashMap<>();\n        Map<String, Integer> id = new HashMap<>();\n        int ans = 0;\n\n        for (int i = 1; i <= n; i++) {\n            int x = nums[i - 1];\n            StringBuilder sb = new StringBuilder();\n            for (int j = 0; j < L; j++) {\n                sb.append((char) (x % 10 + \'0\'));\n                x /= 10;\n            }\n            String s = sb.toString();\n            ans += mp.getOrDefault(s, 0);\n            mp.put(s, mp.getOrDefault(s, 0) + 1);\n            id.put(s, i);\n\n            char[] chars = s.toCharArray();\n            for (int p = 0; p < L; p++) {\n                for (int q = p + 1; q < L; q++) {\n                    if (chars[p] == chars[q]) continue;\n                    swap(chars, p, q);\n                    s = new String(chars);\n                    if (id.getOrDefault(s, 0) < i) {\n                        id.put(s, i);\n                        mp.put(s, mp.getOrDefault(s, 0) + 1);\n                    }\n                    for (int r = p; r < L; r++) {\n                        for (int t = r + 1; t < L; t++) {\n                            if (chars[r] == chars[t]) continue;\n                            swap(chars, r, t);\n                            s = new String(chars);\n                            if (id.getOrDefault(s, 0) < i) {\n                                id.put(s, i);\n                                mp.put(s, mp.getOrDefault(s, 0) + 1);\n                            }\n                            swap(chars, r, t);\n                        }\n                    }\n                    swap(chars, p, q);\n                }\n            }\n        }\n        return ans;\n    }\n\n    private void swap(char[] array, int i, int j) {\n        char temp = array[i];\n        array[i] = array[j];\n        array[j] = temp;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def countPairs(self, nums):\n        maxL = 7\n        n = len(nums)\n        mp = defaultdict(int)\n        ids = {}\n        ans = 0\n\n        for i in range(1, n + 1):\n            x = nums[i - 1]\n            s = \'\'\n            # convert x to a string s and pad with zeros\n            # using str(x).zfill(maxL) also works here\n            for _ in range(maxL):\n                s += chr((x % 10) + ord(\'0\'))\n                x //= 10\n\n            ans += mp[s] # efficiently count the matches.\n            mp[s] += 1\n            ids[s] = i\n\n            s = list(s)\n\n            for p in range(maxL):\n                for q in range(p + 1, maxL):\n                    if s[p] == s[q]:\n                        continue\n                    s[p], s[q] = s[q], s[p]\n                    s_str = \'\'.join(s)\n                    if ids.get(s_str, 0) < i:\n                        ids[s_str] = i\n                        mp[s_str] += 1\n                    for r in range(p, maxL):\n                        for t in range(r + 1, maxL):\n                            if s[r] == s[t]:\n                                continue\n                            s[r], s[t] = s[t], s[r]\n                            s_str = \'\'.join(s)\n                            if ids.get(s_str, 0) < i:\n                                ids[s_str] = i\n                                mp[s_str] += 1\n                            s[r], s[t] = s[t], s[r]\n                    s[p], s[q] = s[q], s[p]\n            \n        return ans\n```
['Python', 'C++', 'Java', 'Python3']