
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to get all the combinations of the keypad value in a mobile by backtracking using C#?
The problem can be broken down into smaller and simple "subproblems", which can be further divided into yet simpler and smaller subproblems. We take each and every digit one by one and count all ndigits reachable from any digit, use a map to store the mapping of digits reachable from every digit. When the digit becomes n-digit, update the count.
Example
using System; using System.Collections.Generic; namespace ConsoleApplication{ public class BackTracking{ private string GetKeyPadValueBasedOnInput(string digit){ Dictionary keypad = new Dictionary(); keypad.Add("2", "abc"); keypad.Add("3", "def"); keypad.Add("4", "ghi"); keypad.Add("5", "jkl"); keypad.Add("6", "mno"); keypad.Add("7", "pqrs"); keypad.Add("8", "tuv"); keypad.Add("9", "wxyz"); return keypad.GetValueOrDefault(digit); } public void FindSequence(string currentList, string digits, List output){ if (digits.Length == 0){ output.Add(currentList); return; } else{ string digit = digits.Substring(0, 1); string letters = GetKeyPadValueBasedOnInput(digit); for (int i = 0; i < letters.Length; i++){ char letter = GetCHarFromString(letters, i); FindSequence(currentList + letter, digits.Substring(1), output); } } } private char GetCHarFromString(string letters, int value){ char[] charArr = letters.ToCharArray(); return charArr[value]; } } class Program{ static void Main(string[] args){ BackTracking b = new BackTracking(); List<string> output = new List<string>(); b.FindSequence("", "34", output); foreach (var item in output){ Console.WriteLine(item); } } } }
Output
dg dh di eg eh ei fg fh fi
- Related Articles
- Print all n digit patterns formed by mobile Keypad in C++
- How to find all the permutation of the string by backtracking using C#?
- Mobile Numeric Keypad Problem\n
- How to get all combinations of some arrays in JavaScript?
- Algorithm to get the combinations of all items in array JavaScript
- How to find the power of any given number by backtracking using C#?
- How to find the distinct subsets from a given array by backtracking using C#?
- How to find the target sum from the given array by backtracking using C#?
- Python program to get all pairwise combinations from a list
- Convert a sentence into its equivalent mobile numeric keypad sequence in C++
- Program to find all possible strings typed using phone keypad in python
- How to get the combinations for a range of values with repetition in R?
- How to get the value of a button in the Entry widget using Tkinter?
- How to get all the Get-Process properties using PowerShell?
- How to get the value of a specific pixel in OpenCV using C++?

Advertisements