Programming beauty phone number corresponds to English Words

Source: Internet
Author: User

Question 1: Based on the correspondence between letters and numbers on the phone, a meaningful word is used to express a phone number, for example, 26678837 using a computer.

Question 2: In turn, can a phone number be expressed in one word? How to express the fastest? Obviously, not all phone numbers can correspond to words.

First, let's take a look at a similar question on leetcode:

Letter combinations of a phone number

Given a digit string, return all possible letter combinations that the number coshould 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"].

Note:
Although the above answer is in lexicographical order, your answer cocould be in any order you want.

Idea: Recursive Implementation is relatively simple. First, a hash table is created, and then a possible value for each number and deep search is obtained. After each number has a value, a result is obtained, the details are as follows:

Class solution {public: void lettercombinations (string & digits, int index, string * map, string words, vector <string> & res) {If (Index = digits. size () // get a result {res. push_back (words); return;} int I; for (I = 0; I <map [digits [Index]-'0']. size (); ++ I) // take each character on the index number for deep recursion {lettercombinations (digits, index + 1, map, words + map [digits [Index]-'0'] [I], Res) ;}} vector <string> lettercombinations (string digits) {string map [10] = {"", "", "ABC", "def", "Ghi", "jkl", "MnO", "pqrs ", "TUV", "wxyz"}; // correspondence between numbers and letters vector <string> res; lettercombinations (digits, 0, map, "", Res ); return res ;}};
According to this question, the above two questions are relatively simple, as mentioned in the beauty of programming: For question 2, we find all possible combinations of the phone and then match the dictionary, determine whether there is an answer. Of course, if you query a large number of times, you can map all words in the dictionary into numbers based on the Conversion Relationship and save them to the file, then, the phone number is shown in the table.


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.