Skip to content

Instantly share code, notes, and snippets.

@kevinwucodes
Created February 11, 2019 01:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinwucodes/0e1e896752a71e53cf026f3eff22cb1a to your computer and use it in GitHub Desktop.
Save kevinwucodes/0e1e896752a71e53cf026f3eff22cb1a to your computer and use it in GitHub Desktop.
daily coding problem #81: Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent

Daily Coding Problem: Problem #81

Good morning! Here's your coding interview problem for today.

This problem was asked by Yelp.

Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.

For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].

Thoughts

  • assuming there isn't a 1 or 0 because those have no mappings on my phone
  • assuming the strict character set of 2-9 as an input (no alphas, no special characters)

if input is empty, return empty string

if the input is a single digit, return the mapping[single digit]

if '23', we need to perform 2 loops, because the length of '23' is two

Psuedocode

input = '23'

input.split('') //['2','3']

const results = [] //here we keep the results

input.forEach((el, index) => ) if it's the first index, we push all the el arrays into the result

if there are more to process, we loop through each result and append the el array's character

finally, we sort the result array

Code

const input = '234'
console.log('answer', possibles(input))

function possibles(input) {
  const mapping = {
    2: ["a", "b", "c"],
    3: ["d", "e", "f"],
    4: ["g", "h", "i"],
    5: ["j", "k", "l"],
    6: ["m", "n", "o"],
    7: ["p", "q", "r", "s"],
    8: ["t", "u", "v"],
    9: ["w", "x", "y", "z"]
  }
  if (!input) return "nothing"  //no input
  if (input.length == 1) return mapping[input]  //single input

  const inputArray = input.toString().split('')
  let results = []

  inputArray.forEach((el, index) => { //['2', '3']
    if (index == 0) {      
      results = results.concat(mapping[el]) //['a','b','c']
    } else {

      //for each el, we replace that el with el + every combination of mapping[el]
      let temp = []

      results.forEach(resultEl => {
        mapping[el].forEach(mappingEl => {
          temp.push(resultEl + mappingEl)
        })
      })

      results = temp
    }
  })

  return results
}
@assault92
Copy link

I try to solved this case with another language. I use VB.net for coding.

Public Sub FungsiRandom2()

		For j As Integer = 0 To 9 - 1

			Dim Alpabet As String = TextBox2.Text
			'------------------------------------------------------'
			Dim length As Integer = Integer.Parse(TextBox1.TextLength)
			Dim listdata As String = String.Empty

			For i As Integer = 0 To length - 1
				Dim karakter As String = String.Empty
				Do
					Dim index As Integer = New Random().Next(0, Alpabet.Length)
					karakter = Alpabet.ToCharArray()(index).ToString()
				Loop While listdata.IndexOf(karakter) <> -1
				listdata += karakter
			Next

			Dim pesan = String.Join(Environment.NewLine, listdata.ToArray())
			ListBox1.Items.Add(pesan)
		Next

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		ListBox1.Items.Clear()
		FungsiRandom2()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
	TextBox1.Clear()
	TextBox2.Clear()
	ListBox1.Items.Clear()
End Sub

Private Sub btn_2_Click(sender As Object, e As EventArgs) Handles btn_2.Click
	TextBox1.Text += "2"
	TextBox2.Text += "abc"
End Sub

Private Sub btn_3_Click(sender As Object, e As EventArgs) Handles btn_3.Click
	TextBox1.Text += "3"
	TextBox2.Text += "def"
End Sub

Private Sub btn_4_Click(sender As Object, e As EventArgs) Handles btn_4.Click
	TextBox1.Text += "4"
	TextBox2.Text += "ghi"
End Sub

Private Sub btn_5_Click(sender As Object, e As EventArgs) Handles btn_5.Click
	TextBox1.Text += "5"
	TextBox2.Text += "jkl"
End Sub

Private Sub btn_6_Click(sender As Object, e As EventArgs) Handles btn_6.Click
	TextBox1.Text += "6"
	TextBox2.Text += "mno"
End Sub

Private Sub btn_7_Click(sender As Object, e As EventArgs) Handles btn_7.Click
	TextBox1.Text += "7"
	TextBox2.Text += "pqrs"
End Sub

Private Sub btn_8_Click(sender As Object, e As EventArgs) Handles btn_8.Click
	TextBox1.Text += "8"
	TextBox2.Text += "tuv"
End Sub

Private Sub btn_9_Click(sender As Object, e As EventArgs) Handles btn_9.Click
	TextBox1.Text += "9"
	TextBox2.Text += "wxyz"
End Sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment