273. Integer to English Words

class Solution {
public:
    string numberToWords(int num) {
       if (num == 0) return "Zero";

        vector<pair<int, string>> units = {
            {1000000000, "Billion"},
            {1000000, "Million"},
            {1000, "Thousand"},
            {1, ""}
        };

        string res;
        for (auto& [val, name] : units) {
            if (num >= val) {
                string segment = helper(num / val);
                res += segment + (name.empty() ? "" : " " + name) + " ";
                num %= val;
            }
        }
        while (!res.empty() && res.back() == ' ') res.pop_back();
        return res;
    }
    string helper(int num) {
        vector<string> below_20 = {
            "", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
            "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
        vector<string> tens = {
            "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
        };
        string res;
        if (num < 20) {
            res = below_20[num];
        } else if (num < 100) {
            res = tens[num / 10];
            if (num % 10 != 0) {
                res += " " + helper(num % 10);
            }
            return res;
        } else {
            res = below_20[num / 100] + " Hundred";
            if (num % 100 != 0) {
                res += " " + helper(num % 100);
            }
        }
        return res;
    }
};