Meta Data#
Difficulty: medium First Attempt: 2026-08-24
- Submissions: Accepted (first try)
Approach#
Despite the Medium label, convert each number to a string, count occurrences with count, and sum. Comparing digit by digit with % 10 also works.
My Solution#
class Solution:
def countDigitOccurrences(self, nums: List[int], digit: int) -> int:
target = str(digit)
return sum(str(x).count(target) for x in nums)
Complexity#
Time O(n · d), space O(d).
Mistakes to Avoid#
digitis an integer; convert it to a string to usestr.count.
Interview Explanation#
I convert each number to a string, count how many times digit appears, and add them up. Without strings, peel digits with % 10 and compare. Time O(n · d).
