[leetcode] Integer to English Words


Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 – 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
class Solution {
public:
    string numbers[10] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    string tennumbers[10] = {"Ten", "Eleven", "Twelve","Thirteen", "Fourteen","Fifteen", "Sixteen","Seventeen","Eighteen","Nineteen"};
    string secondnumbers[10] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    string unit[3] = {"Thousand", "Million", "Billion"};
    string numberToWords(int num) {
        string ans;
        int bNum = num / 1000000000;
        int mNum = (num / 1000000) % 1000;
        int tNum = (num / 1000) % 1000;
        num = num % 1000;
        string bStr = singlePair(bNum);
        
        if(bStr != ""){
            bStr.push_back(' ');
            bStr.append(unit[2]);
            ans.append(bStr);
        }
        
        string mStr = singlePair(mNum);
        if(mStr != ""){
            mStr.push_back(' ');
            mStr.append(unit[1]);
            if(ans != "") ans.push_back(' ');
            ans.append(mStr);  
        } 
        string tStr = singlePair(tNum);
        if(tStr != ""){
            tStr.push_back(' ');
            tStr.append(unit[0]);
            if(ans != "") ans.push_back(' ');
            ans.append(tStr);  
        } 
        string nStr = singlePair(num);
        if(nStr != ""){
            if(ans != "") ans.push_back(' ');
            ans.append(nStr);
        }
        if(ans == "") ans = "Zero";
        return ans;
    }
    string singlePair(int num){
        // less than or equal to 3 digits
        string ans;
        int d1 = num % 10;
        int d2 = (num / 10) % 10;
        int d3 = num / 100;
        if(d3 > 0){
            ans.append(numbers[d3]);
            ans.append(" Hundred");
        }
        if(d2 == 1){
            if(ans != "") ans.push_back(' ');
            ans.append(tennumbers[d1]);
        }
        else if(d2 > 1){
            if(ans != "") ans.push_back(' ');
            ans.append(secondnumbers[d2]);
        }
        if(d1 > 0){
            if(d2 != 1){
                if(ans != "") ans.push_back(' ');
                ans.append(numbers[d1]);
            }
        }
        return ans;
    }
};

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.