Short and Easy to understand Iterative JAVA solution


  • 3
    W
    public class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> res = new ArrayList<String>();
            if (digits == null || digits.length() == 0) return res;
            res.add("");
            String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            for (int i = 0; i < digits.length(); i++){
                List<String> newRes = new ArrayList<String>();
                char[] charArray= mapping[digits.charAt(i) - '0'].toCharArray();
                for (String str : res){
                    for (char c : charArray){
                        newRes.add(new String(str + c));
                    }
                }
                res = newRes;
            }
            return res;
        }
    }

  • 0
    M

    what will happen if the input is 1 or 12?


  • 0
    Y

    When your code run on test case"12", it will output "["1a","1b","1c"]"


Log in to reply
 

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