Count numbers containing 4
Count the numbers between 1 to N containing 4 as a digit.
Example 1:
Input: N = 9 Output: 1 Explanation: 4 is the only number between 1 to 9 which contains 4 as a digit.
Example 2:
Input: N = 14 Output: 2 Explanation: 4 and 14 are the only number between 1 to 14 that contains 4 as a digit.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countNumberswith4() which takes an Integer N as input and returns the answer.
class Solution:
def countNumberswith4(self, N):
s=0
for i in range(1,N+1):
if str(i).count('4')>0:
s=s+1
return s
# code here
Comments
Post a Comment