Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1125 from agnihotriketan/patch-39
Browse files Browse the repository at this point in the history
Create 17-Letter-Combinations-of-a-Phone-Number.cs
  • Loading branch information
Ahmad-A0 authored Sep 17, 2022
2 parents 9acfff3 + bfc767a commit a445a9b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions csharp/17-Letter-Combinations-Of-A-Phone-Number.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Solution {
public IList<string> LetterCombinations(string digits)
{
var lettersMap = new Dictionary<char, string>
{
{'2', "abc"},
{'3', "def"},
{'4', "ghi"},
{'5', "jkl"},
{'6', "mno"},
{'7', "pqrs"},
{'8', "tuv"},
{'9', "wxyz"}
};

var result = new List<string>();

if (!string.IsNullOrEmpty(digits))
Backtrack(result, digits, lettersMap, "", 0);

return result;
}

void Backtrack(List<string> result, string digits, Dictionary<char, string> lettersMap, string curString, int start)
{
if (curString.Length == digits.Length)
{ result.Add(curString); return; }

foreach (var c in lettersMap[digits[start]])
{
Backtrack(result, digits, lettersMap, curString + c, start + 1);
}
}
}

0 comments on commit a445a9b

Please sign in to comment.