Count the Digits That Divide a Number - Modulo 10 [JS]

Description 

Solution: Modulo 10

Find the last digit of the number: n % 10.
Use Math.floor(n / 10) to remove the last digit.
Repeat this until n becomes 0.

Time Complexity: O(log(n)) 58ms
Space Complexity: O(1) 41.7MB

var countDigits = function(num) {
  let n = num, ans = 0;
  while (n > 0) {
    let digit = n % 10;
    if (num % digit === 0) ans++;
    n = Math.floor(n / 10);
  }
  return ans;
};

Comments

Popular posts from this blog

Beautiful Towers II - Monotonic Increasing Stack [JS]

Check if There is a Valid Partition For The Array - Two Approaches - DP & Recursion w/ Memoization [JS]

Divide Nodes Into the Maximum Number of Groups - Union Find & BFS [JS]

Robot Collisions - Stack [JS]

Mice and Cheese - Greedy w/ Sorting [JS]