Java Iterative Solution


  • 0
    public class Solution {
        private final String[] letters =  {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        public List<String> letterCombinations(String digits) {
            List<String> res = new ArrayList<>();
            if (digits == null || digits.length() == 0) return res;
            res.add("");
            for (int i = 0; i < digits.length(); i++) {
                int digit = digits.charAt(i) - '0';
                if (digit < 2) continue;
                String str = letters[digit];
                List<String> temp = new ArrayList<>();
                for (String s : res) {
                    for (char c : str.toCharArray()) {
                        temp.add(s + c);
                    }
                }
                res = temp;
            }
            return res;
        }
    }
    

Log in to reply
 

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