C++ Solution with Explanation and Time Optimizations
Approach\nSame as other solutions. Important points:\n1. Think of 0 as a valid digit. Variations of "1" should include 10,100,1000,etc.\n2. Then, you can think 
# Approach\nSame as other solutions. Important points:\n1. Think of 0 as a valid digit. Variations of "1" should include 10,100,1000,etc.\n2. Then, you can think of equality as symmetric. 1=10 and 10=1.\n3. (c++) Don\'t allocate dynamically sized things in the for loops. The `allOneSwapVariations` thing in the main Java solution caused me to TLE.\n4. (c++) Use a statically allocated `int[]` for tracking the frequency of each number. Using an `unordered_map` caused me to TLE.\n\n# Complexity\n- Time complexity: O(N * D^4)\n\n- Space complexity:1e7\n\n# Code\n```cpp []\ntypedef vector<int> vi;\ntypedef pair<int, int> pii;\n#define ll long long\n#define uoset unordered_set\n#define pb push_back\n#define mp make_pair\n#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))\n#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))\n#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))\n#define FOREACH(a, b) for (auto&(a) : (b))\n#define REP(i, n) FOR(i, 0, n)\n#define REPN(i, n) FORN(i, 1, n)\n#define dbg(x) cout << (#x) << " is " << (x) << endl;\n#define dbg2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << y << endl;\n#define dbgarr(x, sz)                                             \\\n  for (int asdf = 0; asdf < (sz); asdf++) cout << x[asdf] << \' \'; \\\n  cout << endl;\n#define dbgarr2(x, rose, colin)                \\\n  for (int asdf2 = 0; asdf2 < rose; asdf2++) { \\\n    dbgarr(x[asdf2], colin);                   \\\n  }\n#define dbgitem(x)                                                           \\\n  for (auto asdf = x.begin(); asdf != x.end(); asdf++) cout << *asdf << \' \'; \\\n  cout << endl;\n\n// The largest number in nums is 9999999, i.e. we don\'t need to consider numbers with > 7 digits\nconst int pows[8]={1,10,100,1000,(int)1e4,(int)1e5,(int)1e6}, MAXN=1e7;  // pows[i]=10^i\nint frqs[MAXN];\nclass Solution {\n  public: int countPairs(vector < int > & nums) {\n    int res = 0;\n    memset(frqs, 0, sizeof(frqs));\n    for (int num: nums) {\n      res += frqs[num];\n      uoset < int > allVariatons; // using 0, 1, or 2 swaps\n      REP(i, 7) FOR(j, i + 1, 7) { // Swap digits i < j.\n        int di = num / pows[i] % 10, dj = num / pows[j] % 10, x = num - di * pows[i] + dj * pows[i] - dj * pows[j] + di * pows[j]; // 1 swap variation\n        allVariatons.insert(x);\n        REP(i2, 7) FOR(j2, i2 + 1, 7) {     // Swap digits i2 < j2 in x.\n          int di2 = x / pows[i2] % 10, dj2 = x / pows[j2] % 10;\n          allVariatons.insert(x - di2 * pows[i2] + dj2 * pows[i2] - dj2 * pows[j2] + di2 * pows[j2]);       // two swap variation\n        }\n      }\n      for (int variation: allVariatons) frqs[variation]++;\n    }\n    return res;\n  }\n};\n```
['C++']