Interview question: Given a phone number, ask all the letters that are generated (easy to remember phone number)

Source: Internet
Author: User

Question:Sometimes people may wonder why ABC... is on the keyboard of the phone, especially before the birth of the mobile phone. In fact, in addition to sending text messages, these letters are also useful in making phone numbers easy to remember. This is a popular feature in the United States. For example, you can order a pizza from GINO by dialing 310-GINO, GINO corresponds to 4466.

Ha, have fun. Now the question is, if you give a phone number such as 123-4567 or 12345678, ask all of its letters and combinations.

For example, given 23, all combinations are ad, AE, af, bd, be, bf, cd, ce, and cf.

Answer:

Write a recursive implementation.

The general idea is:

1. Create a Mapping table to search for characters corresponding to numbers.

2. If it is a number, return a combination of all possibilities.

3. If there are multiple numbers, return the combination of the last number and the previously produced string (Cartesian Product)

Recursive Implementation
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;

Namespace NumberToWord
{
Class Program
{
Static void Main (string [] args)
{
Foreach (String result in NumberToWord. ConvertNumberToWord ("2543693 "))
{
Console. WriteLine (result );
}
}
}
Class NumberToWord
{
// Mapping Table
// PS: We may use Char [] [] instead here.
// 0
// 1 2 3
// 4 5 6
// 7 8, 9
Private static String [] NumberMappingTable = {
"",
"", "ABC", "DEF ",
"GHI", "JKL", "MNO ",
"PQRS", "TUV", "WXYZ"
};
Public static List <String> ConvertNumberToWord (String number)
{
List <String> resultList = new List <string> ();

// Return an empty list when input is empty.
If (String. IsNullOrEmpty (number. Trim ()))
{
Return resultList;
}

// Use regular expression to test if the input number is valid.
// Valid Characters are: 0123456789 -*#
// Valid Number is like: 342-454-3432
// 4564334
// Length is from 1 to 11
Regex reg = new Regex ("([0-9 * #-]) {1, 11}", RegexOptions. Compiled );
If (! Reg. IsMatch (number ))
{
Throw new ArgumentException ("Number is not well formatted! "," Number ", new ArgumentException ());
}

// Replace '*', '-' and '#' with zero for looking up mapping table.
ResultList = ConvertNumberToWordInternal (number. Replace ('*', '0'). Replace ('#', '0'). Replace ('-', '0 '));

Return resultList;
}
Private static List <String> ConvertNumberToWordInternal (String validNumber)
{
List <String> resultList = new List <string> ();

// When it's the last (from left to right) number,
// Iterate characters it mapped and write them into result List.
If (validNumber. Length = 1)
{
// Get mapped characters
String mappedChars = NumberMappingTable [Convert. ToInt32 (validNumber)];
Foreach (Char ch in mappedChars)
{
ResultList. Add (ch. ToString ());
}
}
// Otherwise we combine the characters mapped by the next (right to left) number
// With the List we already got from resolved numbers.
Else
{
List <String> resolvedList = ConvertNumberToWord (validNumber. Substring (0, validNumber. Length-1 ));
Foreach (String resolvedString in resolvedList)
{
// The next number (from left to right) mapped characters
String nextNumMappedChars = NumberMappingTable [Convert. ToInt32 (validNumber [validNumber. Length-1]. ToString ()];
Foreach (Char ch in nextNumMappedChars)
{
ResultList. Add (resolvedString + ch. ToString ());
}
}
}

Return resultList;
}
}
}

And so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.