# Python Solution - DFS

• ``````class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""

words = {
0: "Zero",
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
11: "Eleven",
12: "Twelve",
13: "Thirteen",
14: "Fourteen",
15: "Fifteen",
16: "Sixteen",
17: "Seventeen",
18: "Eighteen",
19: "Nineteen",
20: "Twenty",
30: "Thirty",
40: "Forty",
50: "Fifty",
60: "Sixty",
70: "Seventy",
80: "Eighty",
90: "Ninety",
100: "Hundred",
1000: "Thousand",
1000000: "Million",
1000000000: "Billion"
}

def build(num):
if num >= 100 and num in words: return "One " + words[num]
if num in words: return words[num]
if num < 100:
return words[int(num / 10) * 10] + " " + words[int(num % 10)]

for d in [1000000000, 1000000, 1000, 100]:
k = int(num / d)
if k > 0:
return build(k) + " " + words[d] + (" " + build(num % d) if num % d != 0 else '')

return build(num)``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.