Super easy to understand Java solution.


  • 0
    P
    class Solution {
        public String numberToWords(int num) {
            if(num == 0) return "Zero";
            return numberToWords(String.valueOf(num)).trim();
            
        }
        
        public String numberToWords(String number){
            // parse the number into integer and conver to string to get rid of 0s from prefix
            number = String.valueOf(Integer.valueOf(number));
            int l = number.length();
            if(l>9) {
                return numberToWords(number.substring(0, l-9)) + " Billion" + numberToWords(number.substring(l-9));
            } else if(l > 6){
                return numberToWords(number.substring(0, l-6)) + " Million" + numberToWords(number.substring(l-6));
            } else if(l >3){
                return numberToWords(number.substring(0, l-3)) + " Thousand" + numberToWords(number.substring(l-3));
            } else if(l>2){
                return numberToWords(number.substring(0, l-2)) + " Hundred" + numberToWords(number.substring(l-2));
            } else if(l==2){
                return handleTwoDigits(number);
            } else if(l==1){
                return handleSingleDigit(number);
            }
            return "";
        }
        
        public String handleSingleDigit(String number){
            switch(number.charAt(0)){
                        case '1':
                            return " One";
                        case '2':
                            return " Two";
                        case '3':
                            return " Three";
                        case '4':
                            return " Four";
                        case '5':
                            return " Five";
                        case '6':
                            return " Six";
                        case '7':
                            return " Seven";
                        case '8':
                            return " Eight";
                        case '9':
                            return " Nine";
                    }
            return "";
        }
        
        public String handleTwoDigits(String number){
            //handle multiple of Ten
            if(number.charAt(1) == '0'){
                    switch(number.charAt(0)){
                        case '0':
                            return "";
                        case '1':
                            return " Ten";
                        case '2':
                            return " Twenty";
                        case '3':
                            return " Thirty";
                        case '4':
                            return " Forty";
                        case '5':
                            return " Fifty";
                        case '6':
                            return " Sixty";
                        case '7':
                            return " Seventy";
                        case '8':
                            return " Eighty";
                        case '9':
                            return " Ninety";
                    }
            } else {
                    // Handle Eleven series
                    if(number.charAt(0) == '1'){    
                            switch(number.charAt(1)){
                                case '1':
                                    return " Eleven";
                                case '2':
                                    return " Twelve";
                                case '3':
                                    return " Thirteen";
                                case '4':
                                    return " Fourteen";
                                case '5':
                                    return " Fifteen";
                                case '6':
                                    return " Sixteen";
                                case '7':
                                    return " Seventeen";
                                case '8':
                                    return " Eighteen";
                                case '9':
                                    return " Nineteen";
                            } 
                    } else {
                        // Word for the Tens Place + Word ofr the Unit Place
                        return  numberToWords(number.substring(0,1) + "0") +   numberToWords(number.substring(1,2))  ;
                    }
            }
            return "";
        }
            
    }
    

Log in to reply
 

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