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]

Minimum Number of Operations to Sort a Binary Tree by Level - BFS & Cycle Counting Explained [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Maximum Score After Applying Operations on a Tree - DFS [JS]

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