Friday, February 28, 2020

Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

class Solution {
public:
    int getLength(int num) {
        int ans = 0;
        while (num != 0) {
            ans ++;
            num /= 10;
        }
        return ans;
    }

    string numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        return numberToWord(num);
    }
    
    // Just a wrapper over numberToWords to handle 0.
    string numberToWord(int num) {
        string ans;
        if (num == 0) {
            return "";
        }
        
        int len = getLength(num);
        
        if (len > 9) {
          ans = helper(num/1000000000) + " Billion";
          string remaining = numberToWord(num % 1000000000);
          if (!remaining.empty()) {
              ans += " " + remaining;
          }
        } else if (len > 6) {
            ans = helper(num / 1000000) + " Million";
            string remaining = numberToWord(num % 1000000);
            if (!remaining.empty()) {
              ans += " " + remaining;
            }
        } else if (len > 3) {
            ans = helper(num / 1000) + " Thousand";
            string remaining = numberToWord(num % 1000);
            if (!remaining.empty()) {
              ans += " " + remaining;
            }
        } else {
            ans = helper(num);
        }
        return ans;
    }
    
    // For num less than 1000.
    string helper(int num) {
        map<int, string> mapOne, mapTwo;
        mapOne = {{1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"},
                  {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"},
                  {9, "Nine"}, {10, "Ten"}, {11, "Eleven"}, {12, "Twelve"},
                  {13, "Thirteen"}, {14, "Fourteen"}, {15, "Fifteen"},
                  {16, "Sixteen"}, {17, "Seventeen"}, {18, "Eighteen"},
                  {19, "Nineteen"}, {20, "Twenty"}};
        mapTwo = {{2, "Twenty"}, {3, "Thirty"}, {4, "Forty"},
                 {5, "Fifty"}, {6, "Sixty"}, {7, "Seventy"},
                 {8, "Eighty"}, {9, "Ninety"}};
        string ans;
        if (num == 0) {
            return "";
        }
        int len = getLength(num);
        if (len == 3) {
            if ((num / 100) > 0) {
                ans = helper(num/100) + " Hundred";
                string remaining = helper(num % 100);
                if (!remaining.empty()) {
                    ans += " " + remaining;
                }
            } else {
                ans = helper(num % 100);
            }
        } else {
            if (num <= 20) {
                return mapOne[num];
            } else {
                if (num / 10) {
                    ans = mapTwo[num / 10];
                    string remaining = helper(num % 10);
                    if (!remaining.empty()) {
                        ans += " " + remaining;
                    }
                }
            }
        }
        return ans;
    }
};

============= Again, but not clean =====
class Solution {
    map<int, string> mapOne = {{0, "Zero"}, {1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"},
                  {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"},
                  {9, "Nine"}, {10, "Ten"}, {11, "Eleven"}, {12, "Twelve"},
                  {13, "Thirteen"}, {14, "Fourteen"}, {15, "Fifteen"},
                  {16, "Sixteen"}, {17, "Seventeen"}, {18, "Eighteen"},
                  {19, "Nineteen"}, {20, "Twenty"}};
        
    map<int, string> mapTwo = {{2, "Twenty"}, {3, "Thirty"}, {4, "Forty"},
                 {5, "Fifty"}, {6, "Sixty"}, {7, "Seventy"},
                 {8, "Eighty"}, {9, "Ninety"}};
public:
    string helper_100(int num) {
        string ans;
        
        if (num < 20) {
            return mapOne[num];
        } else if (20 <= num && num < 100) {
            int first = num / 10;
            int second = num % 10;
            ans = mapTwo[first];
            string temp = mapOne[second];
            if (temp != "Zero") {
                ans += " " + temp;
            }
            return ans;
        } else {
            int first = num / 100;
            ans = mapOne[first] + " Hundred";
            int temp = num % 100;
            
            if (temp >= 20) {
                int temp2 = temp / 10;
                int temp3 = temp % 10;
                if (temp2 != 0) {
                    ans += " " + mapTwo[temp2];
                }
                if (temp3 != 0) {
                    ans += " " + mapOne[temp3];
                }
            } else {
                if (temp != 0) {
                    ans += " " + mapOne[temp];
                }
            }
            return ans;
        }
    }
    
    string numberToWords(int num) {
        string ans;
        if (num < 1000) {
            return helper_100(num);
        } else if (1000 <= num && num < 1000000) {
            string first_half = helper_100(num / 1000);
            string second_half = helper_100(num % 1000);
            if (!first_half.empty()) {
                ans = first_half + " " + "Thousand";
            }
            if (!second_half.empty() && second_half != "Zero") {
                ans += " " + second_half;
            }  
        } else if (1000000 <= num && num < 1000000000) {
            string first_half = helper_100(num / 1000000);
            ans = first_half + " " + "Million";
            int temp = num % 1000000;
            if (temp == 0) {
                return ans;
            }

            string second_half = helper_100(temp / 1000);
            string third_half = helper_100(temp % 1000);
            if (!second_half.empty() && second_half != "Zero") {
                ans += " "+ second_half + " " + "Thousand";
            }
            if (!third_half.empty() && third_half != "Zero") {
                ans += " " + third_half;
            }            
        } else {
            string first_half = helper_100(num / 1000000000);
            ans = first_half + " " + "Billion";
            int temp = num % 1000000000;
            if (temp == 0) {
                return ans;
            }

            string second_half = helper_100(temp / 1000000);
            if (!second_half.empty() && second_half != "Zero") {
                ans += " "+ second_half + " " + "Million";
            }
            
            int temp2 = temp % 1000000; 
            string third_half = helper_100(temp2 / 1000);
            if (!third_half.empty() && third_half != "Zero") {
                ans += " " + third_half + " " + "Thousand";
            }
            string fourth_half = helper_100(temp2 % 1000);
            if (!fourth_half.empty() && fourth_half != "Zero") {
                ans += " " + fourth_half;
            }
        }
        return ans;
    }
};

class Solution {
public:
    vector<string> less_than_20= {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    vector<string> tens= {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    vector<string> thousands= {"", "Thousand", "Million", "Billion"};
    
    string numberToWords(int num) {
        if(num == 0) return "Zero";
        int i= 0;
        string res= "";
        
        while(num> 0){
            if(num%1000 != 0)
                res= helper(num%1000)+ thousands[i]+ " "+ res;
            num /= 1000;
            i++;
        }
        return res.substr(0, res.find_last_not_of(' ')+1);
    }
    
    string helper(int num){
        if(num == 0) return "";
        else if(num < 20)
            return less_than_20[num]+ " ";
        else if(num < 100)
            return tens[num/10]+ " "+ helper(num%10);
        else
            return less_than_20[num/100]+ " Hundred "+ helper(num%100);
    }
};

No comments:

Post a Comment