Convert a non-negative integer num to its English words representation.


Test Cases

Example 1:

Input: num = 123
Output: "One Hundred Twenty Three"

Example 2:

Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Example 3:

Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Constraints:


Solution

class Solution {
    private static final String[] LESS_THAN_20 = {
            "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    };

    private static final String[] TENS = {
            "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
    };

    private static final String[] THOUSANDS = {
            "", "Thousand", "Million", "Billion"
    };

    public String numberToWords(int num) {
        if (num == 0) return "Zero";

        StringBuilder sb = new StringBuilder();
        int i = 0;

        while (num > 0) {
            int chunk = num % 1000;
            if (chunk != 0) {
                StringBuilder part = new StringBuilder();
                helper(chunk, part);
                if (!THOUSANDS[i].isEmpty()) part.append(THOUSANDS[i]).append(" ");
                sb.insert(0, part);
            }
            num /= 1000;
            i++;
        }

        return sb.toString().trim();
    }

    private void helper(int num, StringBuilder sb) {
        if (num == 0) return;

        if (num < 20) {
            sb.append(LESS_THAN_20[num]).append(" ");
        } else if (num < 100) {
            sb.append(TENS[num / 10]).append(" ");
            helper(num % 10, sb);
        } else {
            sb.append(LESS_THAN_20[num / 100]).append(" Hundred ");
            helper(num % 100, sb);
        }
    }
}
Time Complexity: O(1)
Space Complexity: O(1)