Simple Java iterative solution , no recursion, no FIFO queue


  • 0
    S
           String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
           List<String> l1 =  new ArrayList<String>();
           if(digits==null || digits.length()==0){
               return l1;
           }
           
           int num = Integer.parseInt(digits.charAt(0)+"");
           String str = mapping[num];
           
           for(int i =0;i < str.length();i++){
               
               l1.add(str.charAt(i)+"");
           }
           
           int totalDigits = digits.length()-1;
           while(totalDigits >0){
               List<String> l2 =  new ArrayList<String>();
               for(int i =0;i<l1.size();i++){
                   String str1 =  mapping[Integer.parseInt(digits.charAt(digits.length()-totalDigits)+"")];
                   for(int j =0; j<str1.length();j++){
                       String temp = l1.get(i)+""+str1.charAt(j);
                       l2.add(temp);
                   }
               }
               
               l1 =l2;
               totalDigits--;
           }
           
           return l1;

Log in to reply
 

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