Last active
September 27, 2023 10:07
-
-
Save dadhi/801223673f286165ac3a3c75a9760eb3 to your computer and use it in GitHub Desktop.
Phone permutations interview question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
// Image of a telephone's keypad: https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/2880px-Telephone-keypad2.svg.png | |
// Problem: Given phone number, output all possible letter values or "words". | |
// Example: | |
// Input: 123-000 | |
// Output: 1ad-000 1ae-000 1af-000 1bd-000 1be-000 1bf-000 1cd-000 1ce-000 1cf-000 | |
// | |
// Example: | |
// Input: 523 | |
// 5 - jkl | |
// 2 - abc | |
// 3 - def | |
// Output: | |
// jad, jae, jaf, jbd, jbe, jbf, jcd, jce, jcf, | |
// jad, jae, jaf, jbd, jbe, jbf, jcd, jce, jcf, j -> k | |
// 1: 1 | |
// 2: abc | |
// 3: def | |
// 4: ghi | |
// 5: jkl | |
// 6: mno | |
// 7: pqrs | |
// 8: tuv | |
// 9: wxyz | |
// 0: 0 | |
class Solution | |
{ | |
static string[] symbols = new[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", }; | |
static void Main(string[] args) | |
{ | |
// Solution1(); | |
Solution2(); | |
} | |
static void Solution2() | |
{ | |
//const string input = "1"; | |
//const string input = "234"; | |
const string input = "23"; | |
// output: ad, ae, af, bd, be, bf | |
Console.WriteLine($"Input: '{input}'"); | |
List<byte> digits = new(); | |
foreach (var ch in input) | |
{ | |
// filter only valid digits | |
if (byte.TryParse("" + ch, out var d) && d >= 0 & d <= 9) | |
digits.Add(d); | |
else | |
Console.WriteLine("Skipping non digit '{ch}'"); | |
} | |
if (digits.Count == 0) | |
{ | |
Console.WriteLine("No valid digits found"); | |
return; | |
} | |
var firstSymbols = symbols[digits[0]]; | |
if (digits.Count == 1) | |
{ | |
Console.WriteLine($"A single result is {firstSymbols}"); | |
return; | |
} | |
List<string> results = new(); | |
Permutate(digits, results, firstSymbols); | |
Console.WriteLine($"All permutation results are: [{string.Join(", ", results)}]"); | |
static void Permutate(List<byte> digits, List<string> results, string digitSymbols, string res = "", int i = 0) | |
{ | |
++i; | |
if (i >= digits.Count) | |
{ | |
foreach (var s in digitSymbols) | |
results.Add(res + s); | |
} | |
else | |
{ | |
var nextDigitSymbols = symbols[digits[i]]; | |
foreach (var s in digitSymbols) | |
Permutate(digits, results, nextDigitSymbols, res + s, i); | |
} | |
} | |
} | |
static void Solution1() | |
{ | |
const string input = "123"; | |
var n1 = int.Parse("" + input[0]); | |
var n2 = int.Parse("" + input[1]); | |
var n3 = int.Parse("" + input[2]); | |
var s1 = symbols[n1]; | |
var s2 = symbols[n2]; | |
var s3 = symbols[n3]; | |
foreach (var a in s1) | |
{ | |
foreach (var b in s2) | |
{ | |
foreach (var c in s3) | |
{ | |
Console.WriteLine($"{a}{b}{c}"); | |
} | |
} | |
} | |
Console.WriteLine($"{n1}, {n2}, {n3}"); | |
Console.WriteLine($"{s1}, {s2}, {s3}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The newer solution is here: https://dotnetfiddle.net/D6rO5F
For input
23
it outputs: