Letter Combinations of a Phone Number QuestionEditorial Solution–LeetCode

From LeetCode:

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

package tests;

import java.util.HashMap;
import java.util.LinkedList;
import org.testng.annotations.Test;

/**
 * Given a digit string, return all possible letter combinations that the number could represent.
 * @author asharda
 *
 */
public class PhoneTest {

 public void testMapping()
 {
 HashMap<Integer,String> hm=new HashMap<Integer,String>(); 
 hm.put(2, "abc");
 hm.put(3, "def");
 char[]two=hm.get(2).toCharArray();
 char[]three=hm.get(3).toCharArray();
 LinkedList<String> list=new LinkedList<String>();

 for(Character c:two)
 {
 for(Character d:three)
 {

 list.add(String.valueOf(c)+String.valueOf(d));
 }//end of d for
 }//end of c for

 for (String s:list)
 {
 System.out.println(s);
 }
 }
 @Test
 public void testPhoneDictionary() {
 PhoneTest test=new PhoneTest();
 test.testMapping();
 }
}


Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
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