<p>Convert a non-negative integer <code>num</code> to its English words representation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = 123
<strong>Output:</strong> &quot;One Hundred Twenty Three&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = 12345
<strong>Output:</strong> &quot;Twelve Thousand Three Hundred Forty Five&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = 1234567
<strong>Output:</strong> &quot;One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

#include <string>

class Solution {
public:
std::string classes[4] = {"", "Thousand", "Million", "Billion"};

std::string digits[10] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

std::string numbers[10] = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

std::string tens[10] = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

void numberToChunks(int index, int num, std::string & answer) {
    if (num == 0) {
        return;
    }
    std::string str;
    int first = num / 100;
    int second = (num % 100) / 10;
    int third = num % 10;
    if (first != 0) {
        str += digits[first] + " " + "Hundred";
    }
    if (second != 0 and third != 0) {
        if (second == 1) {
            if (!str.empty()) {
                str += " ";
            }
            str += tens[third];
        } else {
            if (!str.empty()) {
                str += " ";
            }
            str += numbers[second] + " " + digits[third];
        }
    } else if (second != 0) {
        if (!str.empty()) {
            str += " ";
        }
        str += numbers[second];
    } else if (third != 0) {
        if (!str.empty()) {
            str += " ";
        }
        str += digits[third];
    }
    if (index != 0) {
        str += " " + classes[index];
    }
    if (answer.empty()) {
        answer = str;
    } else {
        answer = str + " " + answer;
    }
}

std::string numberToWords(int num) {
    if (num == 0) {
        return "Zero";
    }
    std::string answer;
    for (int i = 0; num != 0; ++i) {
        numberToChunks(i, num % 1000, answer);
        num /= 1000;
    }
    return answer;
}
};
9800