Algorithm: Get all possible letter combinations from a dialed number – CodeProject

From Evernote:

Algorithm: Get all possible letter combinations from a dialed number – CodeProject

Clipped from: http://www.codeproject.com/Tips/252963/Algorithm-Get-all-possible-letter-combinations-fro

package insidecoding.android; import java.util.ArrayList; import java.util.List; public class DialSuggestHelper { private static final String[][] MAPPINGS = { { "0" }, { "1" }, { "A", "B", "C" }, { "D", "E", "F" }, { "G", "H", "I" }, { "J", "K", "L" }, { "M", "N", "O" }, { "P", "Q", "R", "S" }, { "T", "U", "V" }, { "X", "Y", "Z", "W" } }; private DialSuggestHelper() { } public static List getConditions(String number) { if (number.startsWith("0") || number.startsWith("1")) { return new ArrayList(); } List list = new ArrayList(); int[] arr = new int[number.length()]; for (int j = 0; j < arr.length; j++) { arr[j] = Integer.parseInt(String.valueOf(number.charAt(j))); } combine("", arr, 0, list); return list; } public static void combine(String root, int[] number, int current, List list) { for (int k = 0; k < MAPPINGS[number[current]].length; k++) { if (current == number.length - 1) { list.add(root + MAPPINGS[number[current]][k]); } else { combine(root + MAPPINGS[number[current]][k], number, current + 1, list); } } } }

Usage: getConditions("564") will return [JMG, JMH, JMI, JNG, JNH, JNI, JOG, JOH, JOI,
KMG, KMH, KMI, KNG, KNH, KNI, KOG, KOH, KOI, LMG,
LMH, LMI, LNG, LNH, LNI, LOG, LOH, LOI].

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s