Problem: You are given an integer N. Among the integers between 1 and N (inclusive), how many _Shichi-Go-San numbers_ (literally "Seven-Five-Three numbers") are there?  Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:    * When the number is written in base ten, each of the digits `7`, `5` and `3` appears at least once, and the other digits never appear.

Submission attempts (in order):
# -*- coding: utf-8 -*-
from sys import stdin
import IPython

d_in = lambda: int(stdin.readline())  # N = d_in()

cand = [0, 3, 5, 7]
N = d_in()


def check_count(count):
    if count[3] > 0 and count[5] > 0 and count[7] > 0:
        return True
    else:
        return False


def search(flag, nonzero, str_n, count):
    
    if len(str_n) == 0:
        if check_count(count):
            return 1
        else:
            return 0
    elif check_count(count) and (not flag):
        return 3**(len(str_n))
    elif not flag:
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            next_count[i] += 1
            zeroflag = nonzero or (i != 0)
            ans += search(False, zeroflag, str_n[1:], next_count)
        return ans
    else:
        next_n = int(str_n[0])
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            if i > next_n:
                break
            elif i == 0:
                zeroflag = nonzero
                ans += search(False, zeroflag, str_n[1:], next_count)
            elif i == next_n:
                next_count[i] += 1
                ans += search(True, True, str_n[1:], next_count)
            else:
                next_count[i] += 1
                ans += search(False, True, str_n[1:], next_count)
        return ans


str_N = list(str(N))
cand_count = {0: 0, 3: 0, 5: 0, 7: 0}
print(search(True, False, str_N, cand_count))
# IPython.embed()


# -*- coding: utf-8 -*-
from sys import stdin

d_in = lambda: int(stdin.readline())  # N = d_in()

cand = [0, 3, 5, 7]
N = d_in()


def check_count(count):
    if count[3] > 0 and count[5] > 0 and count[7] > 0:
        return True
    else:
        return False


def search(flag, nonzero, str_n, count):
    
    if len(str_n) == 0:
        if check_count(count):
            return 1
        else:
            return 0
    elif check_count(count) and (not flag):
        return 3**(len(str_n))
    elif not flag:
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            next_count[i] += 1
            zeroflag = nonzero or (i != 0)
            ans += search(False, zeroflag, str_n[1:], next_count)
        return ans
    else:
        next_n = int(str_n[0])
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            if i > next_n:
                break
            elif i == 0:
                zeroflag = nonzero
                ans += search(False, zeroflag, str_n[1:], next_count)
            elif i == next_n:
                next_count[i] += 1
                ans += search(True, True, str_n[1:], next_count)
            else:
                next_count[i] += 1
                ans += search(False, True, str_n[1:], next_count)
        return ans


str_N = list(str(N))
cand_count = {0: 0, 3: 0, 5: 0, 7: 0}
print(search(True, False, str_N, cand_count))


Verdicts: Runtime Error

Accepted