blob: 6c41e62e1d75670df8c90990b205058618014354 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import List
def numbers_with_sum_of_digits(N: int, S: int) -> List[int]:
def helper(n: int, s: int, current: int) -> List[int]:
if n == 0:
return [current] if s == 0 else []
numbers = []
for i in range(10):
if n == N and i == 0: # First digit can't be 0
continue
if s - i >= 0:
numbers.extend(helper(n - 1, s - i, current * 10 + i))
return numbers
return helper(N, S, 0)
N = 2
S = 4
print(numbers_with_sum_of_digits(N, S)) # Output: [13, 22, 31, 40]
|