Personal Website and Computer Science TUR EN

Interview Question Letter Combinations of a Phone Number

Last update: 1/1/2017 10:23:00 PM


The problem is to have all letter combinations of a given digit in regard to phone keypad. The solution in Java has been provided by the member of LeetCode lirensun.

Problem #17 - Letter Combinations of a Phone Number

Description: 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.

Difficulty: Medium

Solution in Java

Solution by lirensun

package com.fatihkabakci.Medium.LetterCombPhoneNumber;

import java.util.ArrayList;
import java.util.List;

 * @author fkabakci
 * Problem Description: Given a digit string, return all possible letter combinations that the number could represent.
 * Input:Digit string "23"
 * Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 * Note:
 * Although the above answer is in lexicographical order, your answer could be in any order you want.
 * Solution by lirensun:
 * 0. Define a list to store the combination of the characters.
 * 1. Initialize the list by adding an empty value to it.
 * 2. for(i : 0 to number of digits)
 * 	  a. While(the length of the first character in the list equals to the order of digit)
 * 		  b. Use the first element of the list as prefix. Get it.
 * 		  c. By the way, do not forget to make a conversion of the digit from character format to integer.
 * 		     d. for(j : 0 to number of characters in the keypad)
 * 				 e. add prefix (section b) + character (section d) into the list.
 * 3. Return the list.

public class LetterCombinationsPhoneNumber {
	final static String[] keyPad = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
	public List<String> letterCombinations(String digits) {
		List<String> list = new ArrayList<String>();
		if(digits == null || digits.length() == 0)
			return list;
	    for(int i = 0; i < digits.length(); i++) {
	        while(list.get(0).length() == i) {
	            String prefix = list.remove(0);
	            int digit = digits.charAt(i) - "0";
	            for(char character : keyPad[digit].toCharArray())
	            	list.add(prefix + character);
	    return list;

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.