Easy understand Java Solution


  • 18
    A

    method combine is to add new letters to old list, using 2 for-loop.

    for example:

    gave digits = "23"

    i=0 -> result=combine("abc", [""]) ---> [a,b,c];

    i=1 -> result=combine("def", [a,b,c]) ---> [ad,bd,cd, ae,be,ce, af,bf,cf];

      public class Solution {
            public static List<String> letterCombinations(String digits) {
                String digitletter[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
                List<String> result = new ArrayList<String>();
        
                if (digits.length()==0) return result;
                
                result.add("");
                for (int i=0; i<digits.length(); i++) 
                    result = combine(digitletter[digits.charAt(i)-'0'],result);
                
                return result;
            }
            
            public static List<String> combine(String digit, List<String> l) {
                List<String> result = new ArrayList<String>();
                
                for (int i=0; i<digit.length(); i++) 
                    for (String x : l) 
                        result.add(x+digit.charAt(i));
        
                return result;
            }
        }

  • 0
    A

    @annafan ..Looks like it will never enter for (String x: l) since result list is always empty..have you run your code ?


  • 0
    C

    @adityarookey annafan has added "" to the result ArrayList.
    result.add("");
    so it does enter the code .


Log in to reply
 

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