Back to questions
If the numbers from 1 to N are written out in words (e.g., "one", "two", "three", "four", etc.), how many letters are used in total when writing all numbers between 1 and N (inclusive)?
While writing the words, follow these rules:
You can assume that N will always be less than or equal to 1000. Given an integer N, return the total number of letters used when writing the numbers from 1 to N in words.
Input: N = 5
Output: 19
Explanation:
The numbers between 1 and 5 written in words are:
In total, 3 + 3 + 5 + 4 + 4 = 19 letters.
One straightforward way to tackle this problem is to convert every number between 1 and N into its word representation and sum the lengths of all the words. However, the main question is how we are going to convert each number to words.
Converting numbers to words isn’t very hard, but it does require careful attention because there are many special cases. While this might seem tricky at first, the key is to break the problem into smaller steps and build your solution step by step. That's why Tesla asks this question – to see if you'll fold under the pressure, OR if you'll systematically handle all the edge cases.
Let’s start with the simpler cases (0-9) and gradually expand to handle more complex numbers. This approach will help us identify patterns and ensure that we cover all possible scenarios.
We can split the problem into handling different ranges of numbers:
Numbers from 0 to 9:
These are straightforward, and we can directly map each number to its corresponding word. Let’s store these mappings in a dictionary.
Numbers from 10 to 19:
These are irregular because they don’t follow the usual pattern of combining tens and ones. Most numbers follow a pattern, like "twenty-one" (20 + 1) or "thirty-two" (30 + 2). However, numbers like "eleven," "twelve," and "thirteen" are unique cases, so we handle them separately.
Numbers from 20 to 99:
Starting from 20, numbers follow a more predictable pattern. For instance, "twenty-one" to "twenty-nine" combine the tens digit ("twenty") with the ones digit. This pattern continues for other multiples of ten, like "thirty," "forty," etc.
Numbers from 100 to 999:
For numbers above 99, we start with the hundreds digit and append "hundred." If there are additional digits, we append "and" followed by the tens and ones digits.
We are going to work with the digits from left to right. Since we only have one case that results in a thousands digit (1000), we will focus on starting from the hundreds digit:
Hundreds digit:
We start by extracting the hundreds digit using integer division (). Let’s take 982 as an example. Here, gives us 9, which translates to "nine." We append "hundred," resulting in "nine hundred."
Tens and ones digits:
After extracting the hundreds digit, we remove it from the number using modulo (). For 982, this leaves us with , which gives us 82. Before proceeding with processing 82, we need to add "and" after the hundreds digit. This gives us "nine hundred and." Now, we handle the remaining number as follows:
If the remaining number (82) is between 11 and 19, we handle it as a special case using predefined words like "eleven," "twelve," etc., because these numbers do not follow the usual tens pattern. Since 82 is greater than 19, we move on.
Since 82 is 20 or greater, we first determine the tens digit by performing integer division on the remaining number (). This gives us 8. To get the corresponding tens number (80), we multiply 8 by 10, resulting in 80. We then find the word for 80, which is "eighty," and append this to our result.
Now, we check if there is a ones digit remaining. For 82, we find the ones digit using modulo (), which gives us 2. We append the ones digit’s word representation ("two") to the tens representation, creating the final structure: "nine hundred and eighty-two."
So, the word representation of 982 is "nine hundred and eighty-two."
Now, if you are worried about the case where we have the number 1000, you can start from the thousands digit. You would extract it by integer division (), which gives you 1, translating to "one." Since there are no remaining digits, the final word representation for 1000 is simply "one thousand."
Here is the pseudocode for the function that will change numbers to words:
Here is the full code:
In this approach, instead of storing the actual words in a dictionary like we did in the initial approach, we store the lengths of the words directly.
For example, instead of storing "one" for the number 1, we store 3, because "one" has 3 letters. Similarly, instead of storing "eleven" for 11, we store 6 because "eleven" has 6 letters.
Here is the code to do that: