Share 17. letter combinations of a phone number c++,

redfrog901

New member
Số #phone, kết hợp #Letter, #C ++, #Programming ### 17. Kết hợp thư của số điện thoại trong C ++

Trong hướng dẫn này, chúng tôi sẽ học cách tìm tất cả các kết hợp chữ cái có thể của một số điện thoại trong C ++.Chúng tôi sẽ sử dụng số điện thoại sau làm ví dụ:

`` `
1234567890
`` `

Bước đầu tiên là tạo một bản đồ ánh xạ từng chữ số theo các chữ cái tương ứng của nó.Ví dụ: chữ số `1` Bản đồ vào các chữ cái` A`, `B` và` C`.Chữ số `2` ánh xạ vào các chữ cái` d`, `e` và` f`.Và như thế.

`` `C ++
std :: map <char, std :: vector <char >> điện thoại_number_map = {
{'1', {'a', 'b', 'c'}},
{'2', {'d', 'e', 'f'}},
{'3', {'g', 'h', 'i'}},
{'4', {'j', 'k', 'l'}},
{'5', {'m', 'n', 'o'}},
{'6', {'p', 'q', 'r', 's'}},
{'7', {'t', 'u', 'v'}},
{'8', {'w', 'x', 'y', 'z'}},
{'9', {'0'}}
};
`` `

Bây giờ chúng ta có bản đồ, chúng ta có thể bắt đầu tạo ra các kết hợp chữ cái.Chúng tôi sẽ làm điều này bằng cách đi qua đệ quy các chữ số số điện thoại.Đối với mỗi chữ số, chúng tôi sẽ lặp lại danh sách các chữ cái mà nó ánh xạ và tạo ra tất cả các kết hợp có thể có của các chữ cái đó.

`` `C ++
void Generate_letter_combinations (const std :: String & phone_number, int index, std :: vector <std :: string> & kết hợp) {
// Nếu chúng tôi đã kết thúc số điện thoại, chúng tôi đã tìm thấy một sự kết hợp hợp lệ.
if (index == điện thoại_number.length ()) {
kết hợp.push_back (phone_number);
trở lại;
}

// Nhận danh sách các chữ cái mà chữ số hiện tại ánh xạ.
const std :: vector <par> & letters = phone_number_map.at (phone_number [index]);

// tạo ra tất cả các kết hợp có thể của các chữ cái.
for (chữ cái char: chữ cái) {
Generate_letter_combinations (Phone_Number, Index + 1, Kết hợp);
}
}
`` `

Cuối cùng, chúng ta có thể gọi hàm `Generate_letter_combinations ()` để tạo tất cả các kết hợp chữ cái có thể của số điện thoại.

`` `C ++
std :: vector <std :: chuỗi> kết hợp;
Generate_letter_combinations (Phone_Number, 0, kết hợp);
`` `

Đầu ra của chương trình sẽ là một vectơ của chuỗi, mỗi chuỗi chứa một kết hợp chữ cái có thể của số điện thoại.

### hashtags

* #số điện thoại
* #-ký kết thư
* #C ++
* #Programming
* #Recursion
=======================================
#phone number, #Letter combinations, #C++, #Programming ### 17. Letter Combinations of A Phone Number in C++

In this tutorial, we will learn how to find all the possible letter combinations of a phone number in C++. We will use the following phone number as an example:

```
1234567890
```

The first step is to create a map that maps each digit to its corresponding letters. For example, the digit `1` maps to the letters `A`, `B`, and `C`. The digit `2` maps to the letters `D`, `E`, and `F`. And so on.

```c++
std::map<char, std::vector<char>> phone_number_map = {
{'1', {'A', 'B', 'C'}},
{'2', {'D', 'E', 'F'}},
{'3', {'G', 'H', 'I'}},
{'4', {'J', 'K', 'L'}},
{'5', {'M', 'N', 'O'}},
{'6', {'P', 'Q', 'R', 'S'}},
{'7', {'T', 'U', 'V'}},
{'8', {'W', 'X', 'Y', 'Z'}},
{'9', {'0'}}
};
```

Now that we have the map, we can start to generate the letter combinations. We will do this by recursively traversing the tree of phone number digits. For each digit, we will iterate over the list of letters that it maps to and generate all possible combinations of those letters.

```c++
void generate_letter_combinations(const std::string& phone_number, int index, std::vector<std::string>& combinations) {
// If we have reached the end of the phone number, we have found a valid combination.
if (index == phone_number.length()) {
combinations.push_back(phone_number);
return;
}

// Get the list of letters that the current digit maps to.
const std::vector<char>& letters = phone_number_map.at(phone_number[index]);

// Recursively generate all possible combinations of the letters.
for (char letter : letters) {
generate_letter_combinations(phone_number, index + 1, combinations);
}
}
```

Finally, we can call the `generate_letter_combinations()` function to generate all the possible letter combinations of the phone number.

```c++
std::vector<std::string> combinations;
generate_letter_combinations(phone_number, 0, combinations);
```

The output of the program will be a vector of strings, each of which contains a possible letter combination of the phone number.

### Hashtags

* #phone-number
* #Letter-combinations
* #C++
* #Programming
* #Recursion
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top