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.