273. Integer to English Words LeetCode Solution

In this guide, you will get 273. Integer to English Words LeetCode Solution with the best time and space complexity. The solution to Integer to English Words problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Integer to English Words solution in C++
  4. Integer to English Words solution in Java
  5. Integer to English Words solution in Python
  6. Additional Resources
273. Integer to English Words LeetCode Solution image

Problem Statement of Integer to English Words

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

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:

0 <= num <= 231 – 1

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

273. Integer to English Words LeetCode Solution in C++

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

 private:
  const vector<string> belowTwenty{
      "",        "One",     "Two",       "Three",    "Four",
      "Five",    "Six",     "Seven",     "Eight",    "Nine",
      "Ten",     "Eleven",  "Twelve",    "Thirteen", "Fourteen",
      "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
  const vector<string> tens{"",      "",      "Twenty",  "Thirty", "Forty",
                            "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

  string helper(int num) {
    string s;

    if (num < 20)
      s = belowTwenty.at(num);
    else if (num < 100)
      s = tens.at(num / 10) + " " + belowTwenty.at(num % 10);
    else if (num < 1000)
      s = helper(num / 100) + " Hundred " + helper(num % 100);
    else if (num < 1000000)
      s = helper(num / 1000) + " Thousand " + helper(num % 1000);
    else if (num < 1000000000)
      s = helper(num / 1000000) + " Million " + helper(num % 1000000);
    else
      s = helper(num / 1000000000) + " Billion " + helper(num % 1000000000);

    trim(s);
    return s;
  }

  void trim(string& s) {
    s.erase(0, s.find_first_not_of(' '));
    s.erase(s.find_last_not_of(' ') + 1);
  }
};
/* code provided by PROGIEZ */

273. Integer to English Words LeetCode Solution in Java

class Solution {
  public String numberToWords(int num) {
    return num == 0 ? "Zero" : helper(num);
  }

  private final String[] belowTwenty = {"",        "One",     "Two",       "Three",    "Four",
                                        "Five",    "Six",     "Seven",     "Eight",    "Nine",
                                        "Ten",     "Eleven",  "Twelve",    "Thirteen", "Fourteen",
                                        "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
  private final String[] tens = {"",      "",      "Twenty",  "Thirty", "Forty",
                                 "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

  private String helper(int num) {
    StringBuilder s = new StringBuilder();

    if (num < 20)
      s.append(belowTwenty[num]);
    else if (num < 100)
      s.append(tens[num / 10]).append(" ").append(belowTwenty[num % 10]);
    else if (num < 1000)
      s.append(helper(num / 100)).append(" Hundred ").append(helper(num % 100));
    else if (num < 1000000)
      s.append(helper(num / 1000)).append(" Thousand ").append(helper(num % 1000));
    else if (num < 1000000000)
      s.append(helper(num / 1000000)).append(" Million ").append(helper(num % 1000000));
    else
      s.append(helper(num / 1000000000)).append(" Billion ").append(helper(num % 1000000000));

    return s.toString().trim();
  }
}
// code provided by PROGIEZ

273. Integer to English Words LeetCode Solution in Python

class Solution:
  def numberToWords(self, num: int) -> str:
    if num == 0:
      return 'Zero'

    belowTwenty = ['',        'One',       'Two',      'Three',
                   'Four',    'Five',      'Six',      'Seven',
                   'Eight',   'Nine',      'Ten',      'Eleven',
                   'Twelve',  'Thirteen',  'Fourteen', 'Fifteen',
                   'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
    tens = ['',      'Ten',   'Twenty',  'Thirty', 'Forty',
            'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']

    def helper(num: int) -> str:
      if num < 20:
        s = belowTwenty[num]
      elif num < 100:
        s = tens[num // 10] + ' ' + belowTwenty[num % 10]
      elif num < 1000:
        s = helper(num // 100) + ' Hundred ' + helper(num % 100)
      elif num < 1000000:
        s = helper(num // 1000) + ' Thousand ' + helper(num % 1000)
      elif num < 1000000000:
        s = helper(num // 1000000) + ' Million ' + helper(num % 1000000)
      else:
        s = helper(num // 1000000000) + ' Billion ' + helper(num % 1000000000)
      return s.strip()

    return helper(num)
# code by PROGIEZ

Additional Resources

See also  452. Minimum Number of Arrows to Burst Balloons LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.