Given a number n. we need to print all N digit pattern formed by mobile Keypad.
Note: we can move up, down, left, right from any key of mobile keypad and every pattern contain unique key.
Input : N = 3 Output : all 3 digit Pattern are : 123, 125, 145, 147 236, 214, 258, 256, 254 321, 325, 369, 365 412, 456, 452, 458, 478 and so on ..
idea of this solution is based on the DFS. We pick all keypad key as a starting digit for N_digit number one by one, after that we are try to generate all N digit pattern formed by this key( Using DFS because we can only move either up, left, right or down from that key).
PrintPattern Function (DFS Function) Add current key to pattern Pattern += Keypad[x][y] .... make current key as visited visited[x][y] = true; ... Print pattern if size of Pattern == N Call DFS for all 4 adjacent keypad key __DFS_function
Below c++ implementation of above idea
123 125 145 147 236 256 258 254 214 369 365 325 321 456 458 452 478 412 569 563 589 580 587 547 541 523 521 698 658 654 652 632 789 780 785 745 741 896 874 856 854 852 980 987 985 965 963 089 087 085
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Mobile Numeric Keypad Problem
- Recursively print all sentences that can be formed from list of word lists
- Print shortest path to print a string on screen
- Number of groups formed in a graph of friends
- Find size of the largest '+' formed by all ones in a binary matrix
- Size of the Largest Trees in a Forest formed by the given Graph
- Find perimeter of shapes formed with 1s in binary matrix
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Largest component size in a graph formed by connecting non-co-prime nodes
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Given 1's, 2's, 3's ......k's print them in zig zag way.
- Print the matrix diagonally downwards
- Print matrix in zag-zag fashion
- Print all possible paths from the first row to the last row in a 2D array
- Print all neighbour nodes within distance K
- Print all leaf nodes of an n-ary tree using DFS
- Print an N x M matrix such that each row and column has all the vowels in it
- Program to print the Diagonals of a Matrix
- Print completed tasks at end according to Dependencies
- Program to print Spiral Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.