Skip to content

Instantly share code, notes, and snippets.

@ldong
Created November 8, 2017 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldong/8d3a4e2d325b6bf982cd2aac8900d64e to your computer and use it in GitHub Desktop.
Save ldong/8d3a4e2d325b6bf982cd2aac8900d64e to your computer and use it in GitHub Desktop.
Print Phone Number
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
import java.io.*;
import java.util.*;


// new: https://coderpad.io/EK7KCRPA

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

class Solution {
  public static void main(String[] args) {
    ArrayList<Integer> strings = new ArrayList<Integer>();
    strings.add(4);
    strings.add(1);
    strings.add(7);
    HashMap<Integer, char[]> map = new HashMap<Integer, char[]>(); 
    map.put(1, new char[] {'A', 'B', 'T'});
    map.put(4, new char[] {'W', 'P', 'U'});
    map.put(7, new char[] {'X', 'M', 'Z'});
    printL(map, strings);
  }
  
  public static void printL(HashMap<Integer, char[]> map, ArrayList<Integer> L) {
    if(L == null || L.size() < 1) {
      return;
    }
    StringBuilder sb = new StringBuilder();
    int index = 0;
    helper(L, map, sb, 0);
  }
  
  public static void helper(ArrayList<Integer> L, HashMap<Integer, char[]> map, StringBuilder sb, int index) {
    if(index == L.size()) {
      String ss = sb.toString();
      for (int i = 0; i < ss.length(); i++) {
        System.out.print("" + ss.charAt(i));
      }
      System.out.println("");
      return;
    }
    int t = L.get(index);
    char[] cc = map.get(t);
    for (int i = 0; i < L.size(); i++) {
      sb.append(cc[i]);
      helper(L, map, sb, index + 1);
      sb.deleteCharAt(sb.length() - 1);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment