LeetCode 273: Integer to English Words I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!
Problem

Convert a non-negative integer to its English words representation.
Given input is guaranteed to be less than 2^31 - 1.

Inputs
123
1234567891
151
1414312
1234
1241234113

Outputs
&quot;One Hundred Twenty Three&quot;
&quot;One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One&quot;
&quot;One Hundred Fifty One&quot;
&quot;One Million Four Hundred Fourteen Thousand Three Hundred Twelve&quot;
&quot;One Thousand Two Hundred Thirty Four&quot;
&quot;One Billion Two Hundred Forty One Million Two Hundred Thirty Four Thousand One Hundred Thirteen&quot;

Code
#include &lt;string&gt;

class Solution {
    std::string zero_to_twenty[20] = {&quot;Zero&quot;, &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;, &quot;Six&quot;, &quot;Seven&quot;, &quot;Eight&quot;, &quot;Nine&quot;, &quot;Ten&quot;, &quot;Eleven&quot;, &quot;Twelve&quot;, &quot;Thirteen&quot;, &quot;Fourteen&quot;, &quot;Fifteen&quot;, &quot;Sixteen&quot;, &quot;Seventeen&quot;, &quot;Eighteen&quot;, &quot;Nineteen&quot;};
    std::string tens_and_zero[10] = {&quot;Zero&quot;, &quot;Ten&quot;, &quot;Twenty&quot;, &quot;Thirty&quot;, &quot;Forty&quot;, &quot;Fifty&quot;, &quot;Sixty&quot;, &quot;Seventy&quot;, &quot;Eighty&quot;, &quot;Ninety&quot;};
    std::string SPACE = &quot; &quot;;
    std::string BILLION = &quot;Billion&quot;;
    std::string MILLION = &quot;Million&quot;;
    std::string THOUSAND = &quot;Thousand&quot;;
    std::string HUNDRED = &quot;Hundred&quot;;
    std::string ZERO = &quot;Zero&quot;;
    std::string EMPTY_STRING = &quot;&quot;;
    static inline constexpr int one_billion = 1e9;
    static inline constexpr int one_million = 1e6;
    static inline constexpr int one_thousand = 1e3;
    static inline constexpr int one_hundred = 100;
    static inline constexpr int twenty = 20;
    static inline constexpr int ten = 10;

public:
    inline std::string numberToWords(int num) {
        if (num == 0) {
            return ZERO;

        } else {
            std::string words = int2string(num);
            return words.substr(1, words.length() - 1);
        }
    }

private:
    inline std::string int2string(const int n) {
        if (n &gt;= one_billion) {
            return int2string(n / one_billion) + SPACE + BILLION + int2string(n % one_billion);

        } else if (n &gt;= one_million) {
            return int2string(n / one_million) + SPACE + MILLION + int2string(n % one_million);

        } else if (n &gt;= one_thousand) {
            return int2string(n / one_thousand) + SPACE + THOUSAND + int2string(n % one_thousand);

        } else if (n &gt;= one_hundred) {
            return int2string(n / one_hundred) + SPACE + HUNDRED + int2string(n % one_hundred);

        } else if (n &gt;= twenty) {
            return SPACE + tens_and_zero[n / ten] + int2string(n % ten);

        } else if (n &gt;= 1) {
            return SPACE + zero_to_twenty[n];

        } else {
            return EMPTY_STRING;
        }
    }
};

References

Problem

Solution

Discuss
Take a look at what translating
1241234113

results in when aligned slightly differently
                       One       Billion 
Two  Hundred   Forty   One       Million 
Two  Hundred   Thirty  Four      Thousand 
One  Hundred           Thirteen  

you can see that there are several patterns here.

Every number can be chunked into groups of 3 digits like this 1,241,234,113.

Each chunk has a different value for the last column, which is one of &quot;Billion&quot;, &quot;Million&quot;, etc.

Within each chunk, there is one digit at the hundreds place.

Within each chunk, the last 2 digits have a special case till 20, otherwise it's a special word for the ten's place, and the one's place.


So the only variables you need are just
std::string const till_twenty[20] = {&quot;&quot;, &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;, &quot;Six&quot;, &quot;Seven&quot;, &quot;Eight&quot;, &quot;Nine&quot;, &quot;Ten&quot;, &quot;Eleven&quot;, &quot;Twelve&quot;, &quot;Thirteen&quot;, &quot;Fourteen&quot;, &quot;Fifteen&quot;, &quot;Sixteen&quot;, &quot;Seventeen&quot;, &quot;Eighteen&quot;, &quot;Nineteen&quot;};
std::string const tens_place[10] = {&quot;&quot;, &quot;Ten&quot;, &quot;Twenty&quot;, &quot;Thirty&quot;, &quot;Forty&quot;, &quot;Fifty&quot;, &quot;Sixty&quot;, &quot;Seventy&quot;, &quot;Eighty&quot;, &quot;Ninety&quot;};
std::string const thousands[4] = {&quot;&quot;, &quot;Thousand&quot;, &quot;Million&quot;, &quot;Billion&quot;};

Note that I've renamed the variables slightly, and made them const. Also, there's no need for the word Zero, since that will only be written if the entire number is 0.
Now you can build up the functions for the 3 digit chunks
//for the first digit    
std::string hundreds_place(int n) {
   return n ? till_twenty[n] + &quot; Hundred &quot; : &quot;&quot;;
}
  
// for the 2nd and 3rd digit  
std::string ones_and_tens_place(int n) {
    return n &lt;= 20 ? till_twenty[n] : tens_place[n / 10] + 
                                      (n % 10 ? &quot; &quot; + till_twenty[n % 10] : &quot;&quot;)
}

// putting all 3 digits together
std::string by_hundreds(int n) {
    return hundreds_place(n / 100) + ones_and_tens_place(n % 100);
}

Now you can recursively build the 3 digit numbers by keeping track of which power of 1000 is being currently processed
std::string by_thousands(int n, int i) {    // i keeps track of the chunk
    return n ? by_thousands(n / 1000, i + 1) + 
               by_hundreds(n % 1000) + &quot; &quot; 
               + thousands[i] + &quot; &quot;
             : &quot;&quot;;
}

and then finally it can all be put together
std::string numberToWords(int num) {
    std::string res = num ? by_thousands(num, 0) : &quot;Zero  &quot;;
    // with a bit of processing to get rid of trailing spaces
    return res.substr(0, res.length() - 2);        
}