package LinkedList;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class letterDigits {
private static LinkedHashMap<String, String> map;
private static ArrayList<String> deriveWordCombinations(String number) {
ArrayList<String> finalWord = new ArrayList<String>();
ArrayList<String> iterative = new ArrayList<String>();
finalWord.add("");
for (int i = 0; i < number.length(); i++) {
String c = number.substring(i, i + 1);
String stringForNumber = map.get(c);
for (String s : finalWord) {
for (char cs : stringForNumber.toCharArray()) {
iterative.add(s + cs);
}
}
finalWord = iterative;
iterative = new ArrayList<String>();
System.out.println("Final Word->" + finalWord);
}
return finalWord;
}
public void makeHashMap() {
map.put("1", "");
map.put("2", "ABC");
map.put("3", "DEF");
map.put("4", "GHI");
map.put("5", "JKL");
map.put("6", "MNO");
map.put("7", "PQRS");
map.put("8", "TUV");
map.put("9", "WXYZ");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
letterDigits obj = new letterDigits();
map = new LinkedHashMap<String, String>();
obj.makeHashMap();
// System.out.println(map);
String str = "345";
ArrayList<String> word = letterDigits.deriveWordCombinations(str);
System.out.println("Word->" + word);
}
}
Производит продукцию:
Final Word->[D, E, F]
Final Word->[DG, DH, DI, EG, EH, EI, FG, FH, FI]
Final Word->[DGJ, DGK, DGL, DHJ, DHK, DHL, DIJ, DIK, DIL, EGJ, EGK, EGL, EHJ, EHK, EHL, EIJ, EIK, EIL, FGJ, FGK, FGL, FHJ, FHK, FHL, FIJ, FIK, FIL]
Word->[DGJ, DGK, DGL, DHJ, DHK, DHL, DIJ, DIK, DIL, EGJ, EGK, EGL, EHJ, EHK, EHL, EIJ, EIK, EIL, FGJ, FGK, FGL, FHJ, FHK, FHL, FIJ, FIK, FIL]
для ввода "345"