Java solution verbose


  • 0
    C

    I am asked this question in a real interview, not sure if it is acceptable to ask solution for such a tedious problem in 40 mins

        public String numberToWords(int num) {
            List<Integer> nums = new ArrayList<Integer>();
            
           // break the problem into multiple of 1000 
            while(num > 0){
                nums.add(num%1000);
                num /= 1000;
            }
            
            String str = "";
            for(int i=nums.size() - 1; i>=0; i--){
                if(nums.get(i)>0){
                    str += convertToNum(nums.get(i)) + getThousands(i);
                }
            }
            
            if(str.isEmpty()){
                return "Zero";
            }
            
            return str.substring(0,str.length() - 1);
        }
        
        private String convertToNum(int num){
            int[] arr = new int[3];
            int count = 2;
            
            while(num > 0){
                 arr[count --] = num%10;
                 num = num/10;
            }
            
            StringBuilder sb = new StringBuilder();
            if(arr[0] > 0){
                sb.append(getStringRep(arr[0])).append("Hundred ");
            }
            
            if(arr[1] > 0){
                if(arr[1] == 1){
                    // handle teens 
                    if(arr[2] == 0){
                        sb.append("Ten ");
                    }
                    else if(arr[2] == 1){
                        sb.append("Eleven ");
                    }
                    else if(arr[2] == 2){
                        sb.append("Twelve ");
                    }
                    else if(arr[2] == 3){
                        sb.append("Thirteen ");
                    }
                    else if(arr[2] == 5){
                        sb.append("Fifteen ");
                    }
                    else if(arr[2] == 8){
                        sb.append("Eighteen ");
                    }
                    else{
                        sb.append(getStringRep(arr[2])).deleteCharAt(sb.length() - 1).append("teen ");
                    }
                }
                else{
                    sb.append(getTys(arr[1]));
                    
                    if(arr[2] != 0){
                        sb.append(getStringRep(arr[2]));
                    }
                }
            }
            else if(arr[2] > 0){
                sb.append(getStringRep(arr[2]));
            }
            
            return sb.toString();
        }
        
        private String getTys(int num){
            String str = "";
            switch(num){
                case 1 : str = "Ten ";
                    break;
                case 2 : str = "Twenty ";
                    break;
                case 3 : str = "Thirty ";
                    break;
                case 4 : str = "Forty ";
                    break;
                case 5 : str = "Fifty ";
                    break;
                case 6 : str = "Sixty ";
                    break;
                case 7 : str = "Seventy ";
                    break;
                case 8 : str = "Eighty ";
                    break;
                case 9 : str = "Ninety ";
                    break;
            }
            
            return str;
        }
        
        private String getStringRep(int num){
            String str = "";
            switch(num){
                case 1 : str = "One ";
                    break;
                case 2 : str = "Two ";
                    break;
                case 3 : str = "Three ";
                    break;
                case 4 : str = "Four ";
                    break;
                case 5 : str = "Five ";
                    break;
                case 6 : str = "Six ";
                    break;
                case 7 : str = "Seven ";
                    break;
                case 8 : str = "Eight ";
                    break;
                case 9 : str = "Nine ";
                    break;
            }
            
            return str;
        }
        
        private String getThousands(int n){
            String str = "";
            switch(n){
                case 1 : str = "Thousand ";
                    break;
                case 2 : str = "Million ";
                    break;
                case 3 : str = "Billion ";
                    break;
                case 4 : str = "Trillion ";
                    break;
            }
            
            return str;
        }
    

Log in to reply
 

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