# Simply Ruby solution with notes

• ``````=begin
Given a digit string, return all possible letter combinations that
the number could represent. A mapping of digit to letters (just like
on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

https://leetcode.com/problems/letter-combinations-of-a-phone-number/
=end

def letter_combinations(digits)
return [] if digits.length == 0

digits.chars.each_with_object([]) do |d, queue|
chars = i_to_c(d)
next if chars.empty?

if queue.empty?
queue.push *chars
else
queue.length.times do
# important to use .length and not .each -- we're adding
# elements at the bottom, and we don't want no ∞ loop
elem = queue.shift

# append each of the chars[] to each element in the
# queue and stick it to the back of the queue
chars.each {|c| queue << elem + c }
end
end
end
end

def i_to_c(digit)
digit = digit.to_i
return [] if digit < 2

map = ["abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
map[digit - 2].split("")
end

puts letter_combinations("22").inspect
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.