From a7f3cc84a78a2fb9afa297af19e6420bbeaa767c Mon Sep 17 00:00:00 2001 From: Narbe Voskanian Date: Tue, 27 Sep 2022 12:49:57 -0700 Subject: [PATCH 1/2] add #17 Letter Combinations of a Phone Number for ruby --- ...7-Letter-Combinations-of-a-Phone-Number.rb | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ruby/17-Letter-Combinations-of-a-Phone-Number.rb diff --git a/ruby/17-Letter-Combinations-of-a-Phone-Number.rb b/ruby/17-Letter-Combinations-of-a-Phone-Number.rb new file mode 100644 index 000000000..6ea8f4b93 --- /dev/null +++ b/ruby/17-Letter-Combinations-of-a-Phone-Number.rb @@ -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 From 9a010858bfa7e5b0d34ea1bed0dd7dc3c56a3fc6 Mon Sep 17 00:00:00 2001 From: aa0 <71089234+Ahmad-A0@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:53:37 +0100 Subject: [PATCH 2/2] Rename 17-Letter-Combinations-of-a-Phone-Number.rb to 17-Letter-Combinations-Of-A-Phone-Number.rb --- ...hone-Number.rb => 17-Letter-Combinations-Of-A-Phone-Number.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ruby/{17-Letter-Combinations-of-a-Phone-Number.rb => 17-Letter-Combinations-Of-A-Phone-Number.rb} (100%) diff --git a/ruby/17-Letter-Combinations-of-a-Phone-Number.rb b/ruby/17-Letter-Combinations-Of-A-Phone-Number.rb similarity index 100% rename from ruby/17-Letter-Combinations-of-a-Phone-Number.rb rename to ruby/17-Letter-Combinations-Of-A-Phone-Number.rb