
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find all possible strings typed using phone keypad in python
Suppose we have a string containing digits from 2-9. We have to find all possible letter combinations that the number could generate. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does map some characters but no letters.
1 | 2 a b c | 3 d e f |
4 g h i | 5 j k l | 6 m n o |
7 p q r s | 8 t u v | 9 w x y z |
* | 0 | # |
For an example, if the given string is “49”, then the possible strings will be ['gw', 'gx', 'gy', 'gz', 'hw', 'hx', 'hy', 'hz', 'iw', 'ix', 'iy', 'iz']
To solve this, we will follow these steps:
- Define an array called solve to solve the problem recursively
- solve method takes digits, characters, result, current_string and current_level, the function will be like
- if current_level = length of digits, then add current string after the result, and return
- for all characters i in characters[digits[current_level]]
- perform solve(digits, characters, result, current_string + i, current_level + 1)
- The actual function will be like
- if digits length is 0, then return empty list
- define one map to hold numbers and corresponding characters as a string
- result := an empty list
- call solve(digits, characters, result, “”, 0)
Let us see the following implementation to get better understanding:
Example
class Solution(object): def letterCombinations(self, digits): if len(digits) == 0: return [] characters = {2:"abc",3:"def",4:"ghi",5:"jkl",6:"mno",7:"pqrs",8:"tuv",9:"wxyz"} result = [] self.solve(digits,characters,result) return result def solve(self, digits, characters, result, current_string="",current_level = 0): if current_level == len(digits): result.append(current_string) return for i in characters[int(digits[current_level])]: self.solve(digits,characters,result,current_string+i,current_level+1) ob1 = Solution() print(ob1.letterCombinations("49"))
Input
"49"
Output
['gw', 'gx', 'gy', 'gz', 'hw', 'hx', 'hy', 'hz', 'iw', 'ix', 'iy', 'iz']
- Related Articles
- Program to generate all possible strings by taking the choices in python
- Program to find reformatted phone number in Python
- Print all possible words from phone digits in C++
- Program to find maximum possible population of all the cities in python
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Program to find the maximum score from all possible valid paths in Python
- Program to find minimum deletions to make strings strings in Python
- Program to merge strings alternately using Python
- Program to find all possible IP address after restoration in C++
- Python program to accept the strings which contains all vowels
- Python Program – Strings with all given List characters
- Program to find list of all possible combinations of letters of a given string s in Python
- Python - Find all the strings that are substrings to the given list of strings
- Program to find length of longest possible stick in Python?
- Python program to print Possible Words using given characters

Advertisements