my backtrack java solution


  • 0
    L

    String[] mapping = new String[] {"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

    public List<String> letterCombinations(String digits) {
       LinkedList<String> list = new LinkedList<String>();
       if(digits == null || "".equals(digits)){
        return list;
     }
    backtrack(digits,0,"",list);
    return list;
    

    }

      private void backtrack(String digits,int start,String combination,List<String> list){
         if(combination.length() == digits.length()){
         list.add(combination);
         return;
      }
     String numbers = mapping[Integer.valueOf(digits.charAt(start)+"")];
     for(int i = 0; i < numbers.length(); i++){
         backtrack(digits,start+1,combination + numbers.charAt(i),list);
     }
    

    }


Log in to reply
 

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