Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1189 from voski/add-17-letter-combinat…
Browse files Browse the repository at this point in the history
…ions-of-a-phone-number-for-ruby

add neetcode-gh#17 Letter Combinations of a Phone Number for ruby
  • Loading branch information
Ahmad-A0 authored Sep 30, 2022
2 parents e3aaf4b + 9a01085 commit 87bbb42
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ruby/17-Letter-Combinations-Of-A-Phone-Number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
$MAP = {
2 => %w(a b c),
3 => %w(d e f),
4 => %w(g h i),
5 => %w(j k l),
6 => %w(m n o),
7 => %w(p q r s),
8 => %w(t u v),
9 => %w(w x y z)
}

# @param {String} digits
# @return {String[]}
def letter_combinations(digits)
ans = []

recurse(digits, "", ans, 0) unless digits.empty?

ans
end

def recurse(digits, prefix, ans, i)
if digits.length == prefix.length
ans << prefix
return
end

digit = digits[i].to_i

$MAP[digit].each do |c|
recurse(digits, prefix + c, ans, i + 1)
end
end

0 comments on commit 87bbb42

Please sign in to comment.