C# Solution using Dictionary O(n) solution


  • 0
    J

    The trick is, for every three numbers, we add a new name, like, thousand, million, billion. but the three numbers have the same name. For example, 504,709 is Five hundred four thousand seven hundred nine. 504 and 709 get their string representations the same way. The only difference is we add 'thousand' based on the number of digits present.

    We use a dictionary that has some values with unique names. for example, we call 12 as twelve.. not onety two..Below is the code.

    Dictionary<string, string> IntToWordDic = new Dictionary<string, string>();
        
        public string NumberToWords(int num) 
        {
            var result = "";
            InitializeDictionary();
            if (num <= 99) result = TwoDigitHelper(Convert.ToString(num));
            else if (num >= 100 && num <= 999) result = ThreeDigitHelper(Convert.ToString(num));
            else
            {
                var numinstring = Convert.ToString(num);
                switch (numinstring.Length)
                {
                    case 10 :
                        {
                            result = IntToWordDic[numinstring[0]+""] + " Billion ";
                            if (Convert.ToInt32(numinstring.Substring(1,9)) != 0)
                            {
                                result += NumberToWords(Convert.ToInt32(numinstring.Substring(1,9)));
                            }
                        }
                        break;
                    case 9:
                    case 8:
                    case 7:
                        {
                            result += ThreeDigitHelper(numinstring.Substring(0,numinstring.Length-6)) + " Million ";
                            if (Convert.ToInt32(numinstring.Substring(numinstring.Length-6,6)) != 0)
                            {
                                result += NumberToWords(Convert.ToInt32(numinstring.Substring(numinstring.Length-6,6)));
                            }
                        }
                        break;
                    case 6:
                    case 5:
                    case 4:
                        {
                            result += ThreeDigitHelper(numinstring.Substring(0,numinstring.Length-3)) + " Thousand ";
                            if (Convert.ToInt32(numinstring.Substring(numinstring.Length-3,3)) != 0)
                            {
                                result += ThreeDigitHelper(numinstring.Substring(numinstring.Length-3,3));
                            }
                        }
                        break;
                }
            }
            if (result == "" && num == 0) return "Zero";
            return result == "" ? "" : result.Trim();
        }
        
        // For three digit numbers, returns the string equivalent of the number. Number is sent in string format
        private string ThreeDigitHelper(string str)
        {
            var result = "";
           if (Convert.ToInt32(str) == 0) return "";
            //
            // This line was added to handle cases like 001, 01. First we convert these to 1, and then convert them to string 1.
            str = Convert.ToString(Convert.ToInt32(str));
            //
            //
            if (str.Length == 1) return IntToWordDic[str];
            else if (str.Length == 2) return TwoDigitHelper(str);
            else if (str.Length == 3)
            {
                result += IntToWordDic[str[0]+""];
                result += " Hundred ";
                result += TwoDigitHelper(str.Substring(1,2));
            }
            return result == "" ? "" : result.Trim();
        }
        
        // For two digit numbers, returns the string equivalent of the number. Number is sent in string format
        private string TwoDigitHelper(string str)
        {
            var result = "";
            //if ()
            if (Convert.ToInt32(str) == 0) return "";
            //
            // This line was added to handle cases like 001, 01. First we convert these to 1, and then convert them to string 1.
            str = Convert.ToString(Convert.ToInt32(str));
            //
            //
            if (str.Length == 1) return IntToWordDic[str];
            else if (str.Length == 2)
            {
                if (Convert.ToInt32(str) <= 20)
                {
                    if (str[0] == '0') return IntToWordDic[str[1]+""];
                    return IntToWordDic[str];
                }
                else if (Convert.ToInt32(str) % 10 == 0) return IntToWordDic[str];
                else
                {
                    result += IntToWordDic[str[0] + "0"];
                    result += " ";
                    result += IntToWordDic[str[1]+""];
                }
            }
            return result == "" ? "" : result.Trim();
        }
        
        // Dictionary that holds basic/unique numbers and their string equivalent. Used to construct all numbers.
        private void InitializeDictionary()
        {
            IntToWordDic.Clear(); // To make no exception is thrown.
            IntToWordDic.Add("1", "One");
            IntToWordDic.Add("2", "Two");
            IntToWordDic.Add("3", "Three");
            IntToWordDic.Add("4", "Four");
            IntToWordDic.Add("5", "Five");
            IntToWordDic.Add("6", "Six");
            IntToWordDic.Add("7", "Seven");
            IntToWordDic.Add("8", "Eight");
            IntToWordDic.Add("9", "Nine");
            IntToWordDic.Add("10", "Ten");
            IntToWordDic.Add("11", "Eleven");
            IntToWordDic.Add("12", "Twelve");
            IntToWordDic.Add("13", "Thirteen");
            IntToWordDic.Add("14", "Fourteen");
            IntToWordDic.Add("15", "Fifteen");
            IntToWordDic.Add("16", "Sixteen");
            IntToWordDic.Add("17", "Seventeen");
            IntToWordDic.Add("18", "Eighteen");
            IntToWordDic.Add("19", "Nineteen");
            IntToWordDic.Add("20", "Twenty");
            IntToWordDic.Add("30", "Thirty");
            IntToWordDic.Add("40", "Forty");
            IntToWordDic.Add("50", "Fifty");
            IntToWordDic.Add("60", "Sixty");
            IntToWordDic.Add("70", "Seventy");
            IntToWordDic.Add("80", "Eighty");
            IntToWordDic.Add("90", "Ninety");
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.