HackerRank ‘Find Digits’ Solution

Short Problem Definition:

You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.


Find Digits


time complexity is O(N);

space complexity is O(1)


Just follow the problem description. The problem can be optimized by creating a map of digits and their counts.

if __name__ == '__main__':
    t = input()
    for _ in xrange(t):
        a = input()
        count = 0
        for i in list(str(a)):
            if int(i) != 0 and a % int(i) == 0:
                count += 1
        print count

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.