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

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

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

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]