One Response

  1. Ozan Tabak
    Ozan Tabak January 5, 2013 at 9:31 am |

    My Solution in java:


    public class Solution {
    public ArrayList letterCombinations(String digits) {

    if (digits == null) { return null ; }

    HashMap map = new HashMap();

    map.put('2',new char [] {'a','b','c'});
    map.put('3',new char [] {'d','e','f'});
    map.put('4',new char [] {'g','h','i'});
    map.put('5',new char [] {'j','k','l'});
    map.put('6',new char [] {'m','n','o'});
    map.put('7',new char [] {'p','q','r','s'});
    map.put('8',new char [] {'t','u','v'});
    map.put('9',new char [] {'w','x','y','z'});
    map.put('0',new char [] {' '});

    ArrayList resultList = new ArrayList();
    if (digits.isEmpty()) { resultList.add(""); }

    ArrayList tempList = new ArrayList();

    int digLength = digits.length();

    for (int i = 0; i < digLength; i++) {
    Character current = digits.charAt(i);
    if (map.containsKey(current)) {
    char [] chars = map.get(current);
    int resSize = resultList.size();
    if (resSize == 0) {
    for (char repChar : chars) {
    resultList.add(String.valueOf(repChar));
    }
    } else {
    for (int j = 0; j < resSize; j++) {
    String strFromList = resultList.get(j);
    for (char repChar : chars) {
    tempList.add(strFromList + repChar);
    }
    }
    if (tempList.size() < resSize) {
    tempList.addAll(resultList);
    }

    resultList = tempList;
    tempList = new ArrayList();
    }

    }

    }

    return resultList;
    }
    }

    Reply

Leave a Reply