Question: Given a finite number, find out all the possible strings that can be generated by using encoding found on old telephone keypad.
Example: 223 => bad, bae, aae...etc. ( since 2 = abc , 3 = def ... )
Answer: This is a simple recursion problem. We should recurse the loop for each possible character of every digit in given number and append the character to the output. As soon as output length is equal to the input length process/print it.
A small trick is to properly choose function argument, which should allow us to check the current length of the o/p and also the number of characters for current digits.
Code: Ruby code
#!usr/bin/ruby
$array = [" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
def find_comb(input, length, index, out)
if index == length
print out
print "\n"
return
end
for j in 0..($array[input[index].to_i].length-1)
out[index] = $array[input[index].to_i][j]
find_comb(input, length, index+1, out)
end
end
if ARGV[0].index('1') or ARGV[0].index('0')
p "0 and 1 are not allowed."
p "Syntax: ruby telephone.rb number"
exit
end
find_comb(ARGV[0], ARGV[0].length, 0, "")
PS: Follow-up to this question might be to remove duplicate in the output generated.
or
To show only the dictionary words in output.
Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.
I guess solution is already sorted and no duplication because no character is repeating for one number key and input char is also ordered.
May be my understanding with ruby does not enable me to see the duplication/sorting problem. I am pasting here the C++ code for the same. Let me know if I am not doing it right way.
void textcomb(const char * const num, char *str, int idx){
if(strlen(num)==0){
printf("\"%s\"\n",str);
//outs.push_back(str);
return;
}
int charcount=strlen(keypad[num[0]-'0']);
for(int i=0; i< charcount; i++){
str[idx]=keypad[num[0]-'0'][i];
textcomb(num+1,str,idx+1);
str[idx]=NULL;
}
}
@Rahul: Why do you think that we need sorting in here for remove duplicity. There will not be any duplicate as per current logic and we don't need to take care for it. For clarity aab and baa are different.
For example, if my input is 222, answer will be as following w/o duplicates:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc
Hi,
We have the below open position at Amazon India Development Center (Hyderabad).Please share your profile for the same at udayv@amazon.com
Software Development Engineer II - Global Financial Systems
Massive data volume + complex business rules in a highly distributed and service oriented architecture = a world class information collection and delivery challenge. Our challenge is to deliver the software systems which accurately capture, process, and report on the huge volume of financial transactions that are generated each day as millions of customers make purchases, as thousands of Vendors and Partners are paid, as inventory moves in and out of warehouses, as commissions are calculated, and as taxes are collected in hundreds of jurisdictions worldwide.
Vendor Payments Systems (VPS) team in Amazon's Global Financial Systems Org is seeking an exemplary Software Development Engineer with broad technical skills to help us build our next generation extranet applications for Amazon Vendors and a centralized intranet portal for day to day activities of internal business customers. Vendor Payments Systems team is completely located in Hyderabad, India and owns the invoice and payment processing for worldwide Amazon Vendors. Billions of transactions that are handled in this space give us a unique opportunity to innovate on both technology and business front and helps to improve free cash flows of company.
The ideal candidate will draw upon exemplary analytical, critical thinking and problem solving skills, deep software development experience, and a passion for creating maintainable, highly reliable and scalable user facing applications that are accessed by thousands of external Vendors and internal Customers. Successful members of this team collaborate effectively with internal customers; other dependent development teams in Amazon and technical support/sustaining engineering teams to develop new applications successfully against high operational standards of system availability and reliability. We look for engineers who are excellent communicators, self-motivated, flexible, hardworking, and who like to have fun. In the long run, this candidate is also required to lead development team that works on vendor facing/customer facing applications.
Qualifications:
• BS or MS degree in Computer Science and 4+ years of industry experience developing large scale enterprise software.
• Solid proficiency with an OO language (preferably Java) and design patterns.
• Experience with RDBMS scaling challenges, data modeling, XML, XSLT.
• Experience in developing highly scalable & available web services and user interfaces. Experience with Ruby on Rails is preferred. (Not Mandatory)
• Penchant to work with business customers and innovate on business processes.
• Excellent written and verbal communication skills.
• Maniacal customer obsession.
Preferred Qualifications:
Experience in Managing small to medium size development teams are preferred still being an IC role.(Not Mandatory)
Thanks & Regards,
Uday Kiran
You performed a fantastic job! This is a fantastic article. This is quite appealing to me. It is quite helpful to my research. It's evident that you're passionate about the subject. I'm hoping you'll give me more information about the software. Please keep spreading the word. Custom Website Design Company
Great article! I found the insights shared here really helpful and informative
Alex Hales| cbn sleep gummies