In the Leetcode Letter Combinations of a Phone Number problem solution in C# programming Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Leetcode Letter Combinations of a Phone Number problem solution in C# programming
public class Solution {
List<string> list = new List<string>();
public IList<string> LetterCombinations(string digits) {
if(digits == "") return list;
helper(0, digits, new string(""));
return list;
}
public void helper(int i, string digits, string singleCom)
{
if(digits.Length == singleCom.Length)
{
list.Add(singleCom);
return;
}
if(i >= digits.Length || singleCom.Length >= digits.Length){
return;
}
string code = getCode(digits[i]);
foreach(char item in code)
{
singleCom += item.ToString();
helper(i + 1, digits, singleCom);
singleCom = singleCom.Remove(singleCom.Length - 1);
}
}
public String getCode(char n){
if(n == '2'){
return "abc";
}else if( n == '3') {
return "def";
}else if( n == '4'){
return "ghi";
}else if( n == '5'){
return "jkl";
}else if( n == '6'){
return "mno";
}else if( n == '7'){
return "pqrs";
}else if( n == '8'){
return "tuv";
}else if( n == '9'){
return "wxyz";
}else{
return "";
}
}
}
Also read,
- Leetcode Letter Combinations of a Phone Number problem solution in C++
- Leetcode Letter Combinations of a Phone Number problem solution in Java
- Leetcode Letter Combinations of a Phone Number problem solution in Python
- Leetcode Letter Combinations of a Phone Number problem solution in C