Sunday, January 27, 2013

Permutation of possible string using a phone keypad, Java

Print all possible combinations of letters that a particular phone number could represent.
<pre class="brush:java">

import java.util.*;

public class permutation {
public static String[] mapping1[] = {{"0"},{"1"},{"A","B","C"},{"D","E","F"},{"G","H","I"},{"J","K","L"},{"M","N","O"},
{"P","Q","R","S"},{"T","U","V"},{"W","X","Y","Z"}};

public List<String> GetPermutation(String phoneNumber)
{
List<String> combo = new ArrayList<String>();
GeneratePermutation(combo, "", phoneNumber);
return combo;
}
public static void main(String[] args)
{
String phonenumber = "3456789";
permutation abc = new permutation();
List<String> comboList = abc.GetPermutation(phonenumber);

for(String permutation: comboList)
{
System.out.println(permutation);
}
}

public void GeneratePermutation(List<String> combo, String prefix, String endPhone){
Integer currentInt = Integer.parseInt(endPhone.substring(0,1));

if(endPhone.length() == 1)
{
// Reached string end add all possible combination
for(int temp = 0; temp < mapping1[currentInt].length; temp++)
{
combo.add(prefix + mapping1[currentInt][temp]);
}
}
else
{
for(int temp = 0; temp < mapping1[currentInt].length; temp++)
{
GeneratePermutation(combo, prefix + mapping1[currentInt][temp], endPhone.substring(1));
}
}
}
}
</pre>

Major intention of this post is to test the new script in blogger which can display code block. But it does not seems to work. Will update it soon.

Credits: http://stackoverflow.com/questions/2344496/how-can-i-print-out-all-possible-letter-combinations-a-given-phone-number-can-re

No comments:

Post a Comment