Java Solution with simple BFS


  • 0
    R

    """
    public class Solution {

    public List<String> letterCombinations(String digits) {
        String[] mapping = {"0","0","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        LinkedList<String> result = new LinkedList<>();
        if(digits.length()==0) return (List<String>)result;
        result.add("");
        while(!result.isEmpty()){
            String str = result.getFirst();
            if(str.length()==digits.length())break;
            for(char c : mapping[digits.charAt(str.length())-'0'].toCharArray()){
                String s = result.getFirst();
                s += c;
                result.add(s);
            }
            result.removeFirst();
        }
        return (List<String>)result;
        
    }
    

    }
    """


Log in to reply
 

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