Count Occurrences of a Digit
EasyP3Coding
Given a non-negative integer n and a digit d (0 to 9), return how many times d appears in the decimal representation of n.
The number 0 is written with one digit, so countDigit(0, 0) is 1.
Example 1
- Input
- n = 1223334, d = 3
- Output
- 3
Example 2
- Input
- n = 0, d = 0
- Output
- 1
Example 3
- Input
- n = 0, d = 5
- Output
- 0
Constraints
- 0 <= n <= 10^15
- 0 <= d <= 9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.