title: How to check if 2 numbers have same digits and length?
body: <p>The following code snippets are 2 algorithms I have written to ascertain if 2 numbers have the same digits and length. For example:</p>
  <ul>
  <li>12 &amp; 21 have same digits and same length, so the output will be true</li>
  <li>12 &amp; 121 have same digits but not same length, so the output will be false</li>
  </ul>
  <p>If anything is not clear, please comment and I will add more details.</p>
  <p>Is there a better way to do this check? Better in any aspect: efficiency (time and/or space), code readability, corner cases handling etc. For example, the first approach has the risk of integer overflow but could be an OK approach as long as the numbers are not large, because the time-complexity is linear.
  Appreciate the feedback.</p>
  <ol>
  <li>Hash with primes:</li>
  </ol>
  <ul>
  <li><p>in this approach, we're using a static array of 1st 10 prime numbers as a helper structure</p>
  </li>
  <li><p>we then compute hash numbers for both input-numbers using the prime numbers array</p>
  </li>
  <li><p>if value of both hash is same, implies the numbers have same digits and length</p>
  <pre class="lang-java prettyprint-override"><code>public static boolean haveSameDigitsAndLengthPrimes(int a, int b) {
      int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
      int hashA=1, hashB=1;
      while (a &gt; 0) {
          hashA *= primes[a%10];
          a /= 10;
      }
      while (b &gt; 0) {
          hashB *= primes[b%10];
          b /= 10;
      }
      return (hashA == hashB);
  }
  </code></pre>
  </li>
  </ul>
  <ol start="2">
  <li>Count digits approach:</li>
  </ol>
  <ul>
  <li><p>in this approach, we're using a helper array of size 10 (initial value of each element = 0)</p>
  </li>
  <li><p>we then iterate on both the numbers; while doing so, increment the array digit by 1 for first number and decrement by 1 for second number</p>
  </li>
  <li><p>after the iteration if any of the number is still not zero, implies their length is not the same</p>
  </li>
  <li><p>if any value in the helper array is not equal to 0, implies the 2 numbers are not satisfying the required conditions</p>
  <pre class="lang-java prettyprint-override"><code>public static boolean haveSameDigitsAndLength(int a, int b) {
      int[] digits = new int[10];
      for (int i=a; i&gt;0; i=i/10) {
          ++digits[i%10];
      }
      for (int i=b; i&gt;0; i=i/10) {
          --digits[i%10];
      }
      for (int digit : digits) {
          if (digit != 0) {
              return false;
          }
      }
      return true;
  }
  </code></pre>
  <p>2b. Count digits approach: implementation improved:</p>
  <pre class="lang-java prettyprint-override"><code>public static boolean haveSameDigitsAndLength(int a, int b) {
      if ((a == 0 || b == 0) &amp;&amp; a != b) {
          return false;
      }
      int[] digits = new int[10];
      int i=a, j=b;
      for (; i&gt;0 &amp;&amp; j&gt;0; i/=10, j/=10) {
          ++digits[i%10];
          --digits[j%10];
      }
      if (i != 0 || j != 0) {
          return false;
      }
      for (int digit : digits) {
          if (digit != 0) {
              return false;
          }
      }
      return true;
  }
  </code></pre>
  </li>
  </ul>
  <p>Test Cases:</p>
  <ul>
  <li>12, 12 =&gt; true</li>
  <li>12, 21 =&gt; true</li>
  <li>123, 132 =&gt; true</li>
  <li>123, 1234 =&gt; false</li>
  <li>10, 1 =&gt; false</li>
  </ul>