Skip to content

2520. Count the Digits That Divide a Number 👍

  • Time: $O(\log\texttt{num})$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int countDigits(int num) {
    int ans = 0;

    for (int n = num; n > 0; n /= 10)
      if (num % (n % 10) == 0)
        ++ans;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int countDigits(int num) {
    int ans = 0;

    for (int n = num; n > 0; n /= 10)
      if (num % (n % 10) == 0)
        ++ans;

    return ans;
  }
}
1
2
3
class Solution:
  def countDigits(self, num: int) -> int:
    return sum(num % int(d) == 0 for d in str(num))