Integer to English Words (Hard)
Here is a hard problem to convert a number to English words: https://leetcode.com/problems/integer-to-english-words/
The problem is laborious (in terms of effort to write a lot of boring code) but actually not that hard if you use one technique: create a helper function that converts a number with up to three digits to English words. This is going to be the most laborious and boring work. But it can be done with the help of a couple of hash tables plus some careful parsing of the number. Once you write it (and test it well!), loop thru the original number in blocks of three calling the helper function and writing a potential suffix (such as "Thousand"). Code is down below, cheers, ACC.
public class Solution
{
public string NumberToWords(int num)
{
string[] suffix = { "", " Thousand", " Million", " Billion", " Trillion" };
string retVal = "";
int index = 0;
if (num == 0) return "Zero";
while (num > 0)
{
string part = Block3ToWord((num % 1000).ToString());
if (part != "")
{
retVal = " " + part + suffix[index] + retVal;
}
index++;
num /= 1000;
}
return retVal.Trim();
}
private string Block3ToWord(string num)
{
if (num.Equals("0")) return "";
Hashtable map = new Hashtable();
map.Add(1, "One");
map.Add(2, "Two");
map.Add(3, "Three");
map.Add(4, "Four");
map.Add(5, "Five");
map.Add(6, "Six");
map.Add(7, "Seven");
map.Add(8, "Eight");
map.Add(9, "Nine");
map.Add(10, "Ten");
map.Add(11, "Eleven");
map.Add(12, "Twelve");
map.Add(13, "Thirteen");
map.Add(14, "Fourteen");
map.Add(15, "Fifteen");
map.Add(16, "Sixteen");
map.Add(17, "Seventeen");
map.Add(18, "Eighteen");
map.Add(19, "Nineteen");
Hashtable map2 = new Hashtable();
map2.Add(20, "Twenty");
map2.Add(30, "Thirty");
map2.Add(40, "Forty");
map2.Add(50, "Fifty");
map2.Add(60, "Sixty");
map2.Add(70, "Seventy");
map2.Add(80, "Eighty");
map2.Add(90, "Ninety");
string retVal = "";
if (num.Length == 3)
{
retVal = (string)map[num[0] - '0'] + " Hundred";
num = num.Substring(1);
}
if (num.Length == 2)
{
if (map.ContainsKey(Int32.Parse(num)))
{
retVal += " " + (string)map[Int32.Parse(num)];
return retVal.Trim();
}
else
{
string temp = num[0].ToString() + "0";
retVal += " " + (string)map2[Int32.Parse(temp)];
num = num.Substring(1);
}
}
if (num.Length == 1)
{
retVal += " " + (string)map[num[0] - '0'];
}
return retVal.Trim();
}
}
273. Integer to English Words
Hard
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"
Accepted
138,107
Submissions
537,502
The problem is laborious (in terms of effort to write a lot of boring code) but actually not that hard if you use one technique: create a helper function that converts a number with up to three digits to English words. This is going to be the most laborious and boring work. But it can be done with the help of a couple of hash tables plus some careful parsing of the number. Once you write it (and test it well!), loop thru the original number in blocks of three calling the helper function and writing a potential suffix (such as "Thousand"). Code is down below, cheers, ACC.
public class Solution
{
public string NumberToWords(int num)
{
string[] suffix = { "", " Thousand", " Million", " Billion", " Trillion" };
string retVal = "";
int index = 0;
if (num == 0) return "Zero";
while (num > 0)
{
string part = Block3ToWord((num % 1000).ToString());
if (part != "")
{
retVal = " " + part + suffix[index] + retVal;
}
index++;
num /= 1000;
}
return retVal.Trim();
}
private string Block3ToWord(string num)
{
if (num.Equals("0")) return "";
Hashtable map = new Hashtable();
map.Add(1, "One");
map.Add(2, "Two");
map.Add(3, "Three");
map.Add(4, "Four");
map.Add(5, "Five");
map.Add(6, "Six");
map.Add(7, "Seven");
map.Add(8, "Eight");
map.Add(9, "Nine");
map.Add(10, "Ten");
map.Add(11, "Eleven");
map.Add(12, "Twelve");
map.Add(13, "Thirteen");
map.Add(14, "Fourteen");
map.Add(15, "Fifteen");
map.Add(16, "Sixteen");
map.Add(17, "Seventeen");
map.Add(18, "Eighteen");
map.Add(19, "Nineteen");
Hashtable map2 = new Hashtable();
map2.Add(20, "Twenty");
map2.Add(30, "Thirty");
map2.Add(40, "Forty");
map2.Add(50, "Fifty");
map2.Add(60, "Sixty");
map2.Add(70, "Seventy");
map2.Add(80, "Eighty");
map2.Add(90, "Ninety");
string retVal = "";
if (num.Length == 3)
{
retVal = (string)map[num[0] - '0'] + " Hundred";
num = num.Substring(1);
}
if (num.Length == 2)
{
if (map.ContainsKey(Int32.Parse(num)))
{
retVal += " " + (string)map[Int32.Parse(num)];
return retVal.Trim();
}
else
{
string temp = num[0].ToString() + "0";
retVal += " " + (string)map2[Int32.Parse(temp)];
num = num.Substring(1);
}
}
if (num.Length == 1)
{
retVal += " " + (string)map[num[0] - '0'];
}
return retVal.Trim();
}
}
Comments
Post a Comment