Mark As Completed Discussion

Good evening! Here's our prompt for today.

Can you convert a number to its text equivalent? This is otherwise known as number spelling.

Description

For example, given 1234, we expect to get "one thousand two hundred and thirty four" back.

Constraints

  • The given number will have <= 100 digits
  • Let n be the number of digits
  • Expected time complexity : O(n)
  • Expected space complexity : O(n)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

We'll now take you through what you need to know.

How do I use this guide?

This problem is deceivingly complicated, so let's take it step and step. Let's use a simple number, like 123 for now.

First, let's convert 123 to a string type to start our operation.

TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Then check if it's zero (because if it is, we can just return "zero").

Next, let's make sure we've defined the building blocks that will make up the final string. They include:

  • units like one, thirteen, etc.
  • tens values like twenty, thirty, etc.
  • scales like thousand, and million

Now we account for the various scales. Most of the scaling levels come up every 3 digits. Using this code block, we can get every 3 digits as chunks.

Step Three
TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

All it does is start from right to left, grabbing every 3 digits and pushing them into a chunks array.

We then adjust for the scale we're at, and make sure we have enough digits to justify that scale.

Now, here's the key. For each chunk, we process it in this manner:

  1. Split it into individual integers ("123" becomes [1, 2, 3])
  2. Add tens word if array item exists
  3. Add 'and' string after units or tens integer (optional)
  4. Add hundreds word if array item exists
Complexity Of Final Solution

Complexity of Final Solution

Let n be the number of digits in the input num. We iterate through the string equivalent of num, and examining 3 digits at a time for O(n) linear time complexity. When converting nums to a string of length n, we have O(n) space complexity.

One Pager Cheat Sheet

  • You can convert a number to its text equivalent using number spelling in O(n) time and O(n) space complexity, where n is the number of digits and the number of digits must not exceed 100.
  • We can convert the number 123 to a string type to start our operation.
  • We need to define units, tens and scales for building the final string, and then divide the number into chunks of 3 digits to account for different scales.
  • We iterate through num in O(n) linear time, and convert it to a string of length n for O(n) space complexity.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

That's all we've got! Let's move on to the next tutorial.

If you had any problems with this tutorial, check out the main forum thread here.