Easy java solution


  • 0
    A
    static String[] phone = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        
        List<String> list = new ArrayList<String>();
        if(digits.length() == 0 || digits == null){
            return list;
        }
        list.add("");
        combinate(digits, list);
        return list;
    }
    
    public static void combinate(String digits, List<String> list){
        if(digits.length() == 0 || digits == ""){
            return;
        }
         String tmp = phone[Integer.parseInt(digits.charAt(0) + "")];
         int listSize = list.size();
         for(int i = 0; i < listSize; i++){
             String l = list.get(i);
             for(int j = 1; j < tmp.length(); j++){
                 char c = tmp.charAt(j);
                 list.add(l + c + "");
             }
             list.set(i, l + tmp.charAt(0) + "");
         }
         digits = digits.substring(1);
         combinate(digits, list);
    }

Log in to reply
 

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