Simple Java DFS


  • 0
    S

    public class Solution {

    static String[] mapping = new String[]{"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    
    public List<String> letterCombinations(String digits) {
        List<String> lst = new LinkedList<String>();
        if(digits.equals(""))
            return lst;
        dfs(lst, digits, "", 0);
        return lst;
    }
    
    private void dfs(List<String> lst, String digits, String cur, int start){
        if(start==digits.length()){
            lst.add(cur);
        }
        else{
            char digit = digits.charAt(start);
            String str = mapping[digit-'0'];
            char[] chars = str.toCharArray();
            for(char c : chars){
                dfs(lst, digits, cur+c, start+1);
            }
        }
    }
    

    }


Log in to reply
 

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