当前位置: 首页 > news >正文

Letter Combination of a Phone Number

Introduce

在这里插入图片描述

Problem Analysis (Using “258” as example)

//2   a    b    c
//5   j    k    l
//8   t    u    v

Possible letter combinations:

  • a, j, t (no further options, this is one combination)
  • a, j, u (no further options, another combination)
  • a, j, v (another combination)
  • a, k, t (another combination)

this resembles a depth-first search (DFS) traversal of an n-ary tree.

Solution Approach

To traverse all combinations starting with ‘a’, we need to traverse all subtrees rooted at ‘j’, ‘k’, and ‘l’ (the next level letters for digit ‘5’)

Similarly, to traverse combinations starting with ‘j’, we need to traverse subtrees rooted at ‘t’, ‘u’, and ‘v’ (the next level letter for digit ‘8’)

Recursive implementation

class Solution {
public:vector<string> letterCombinations(string digits) {}
};

We`ll implement a recursive solution:

class Solution {
public:void _letterCombinations(string digits){}vector<string> letterCombinations(string digits) {}
};

Digit-to-Letter Mapping

Since we`re dealing with digit 2-9, we can store the corresponding letters in an string array for O(1) lookup:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:void _letterCombinations(string digits){}vector<string> letterCombinations(string digits) {}
};

Tree Traversal Implementation

Now we implement the traversal starting from digits[0]:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i){// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1);}}vector<string> letterCombinations(string digits) {// Start traversal from level 0_letterCombinations(digits, 0);}
};

Adding Recursion Boundary Condition

we must add a termination condition for the recursion:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i){// Recursion boundaryif (i == digits.size()) {return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1);}}vector<string> letterCombinations(string digits) {// Start traversal from level 0_letterCombinations(digits, 0);}
};

Storing Results

We need to store the combinations in a vector:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1, ret);}}vector<string> letterCombinations(string digits) {vector<string> result;// Start traversal from level 0_letterCombinations(digits, 0, result);return result; // Don't forget to return!}
};

Building Combinations

We need to build the combinations by passing the current combination down the recursion:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, string current, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {ret.push_back(current);return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level with updated combination_letterCombinations(digits, i + 1, current + str[j], ret);}}vector<string> letterCombinations(string digits) {vector<string> result;string current;// Start traversal from level 0_letterCombinations(digits, 0, current, result);return result;}
};

Edge Case Handling

Finally, we handle the empty input case:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, string current, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {ret.push_back(current);return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level with updated combination_letterCombinations(digits, i + 1, current + str[j], ret);}}vector<string> letterCombinations(string digits) {vector<string> result;if (digits.empty()) {return result;}string current;// Start traversal from level 0_letterCombinations(digits, 0, current, result);return result;}
};
http://www.lryc.cn/news/593033.html

相关文章:

  • 【Bluedroid】btif_av_sink_execute_service之服务器启用源码流程解析
  • 模块加载、ES、TS、Babel 浅析
  • Gerrit workflow
  • 云边端协同架构下的智能计算革命
  • 打造高效订单处理!ZKmall开源商城的统一履约中心架构解析
  • 车载诊断架构 --- 故障码DTC严重等级定义
  • 模电基础-电阻和功率
  • Oracle Database 23ai 技术细节与医疗 AI 应用
  • python学智能算法(二十五)|SVM-拉格朗日乘数法理解
  • 车载诊断架构 --- OEM对于DTC相关参数得定义
  • 开疆智能Profinet转ModbusTCP网关连接康耐视InSight相机案例
  • VUE2 学习笔记1
  • python爬虫之获取渲染代码
  • 【机器学习深度学习】为什么要将模型转换为 GGUF 格式?
  • 计算机网络:(十一)多协议标记交换 MPLS
  • 结合python面向对象编程,阐述面向对象三大特征
  • 软件设计师之开发模型
  • HTML5中的自定义属性
  • 从Prompt到结构建模:如何以数据驱动重构日本语言学校体系?以国际日本语学院为例
  • World of Warcraft [CLASSIC] The Ruby Sanctum [RS] Halion
  • 在 .NET Core 中创建 Web Socket API
  • Kotlin泛型约束
  • NLP中情感分析与观念分析、价值判断、意图识别的区别与联系,以及四者在实际应用中的协同
  • RabbitMQ—事务与消息分发
  • espidf启用vTaskList方法
  • 使用MATLAB探索圆周率π的奇妙计算之旅
  • day25 力扣90.子集II 力扣46.全排列 力扣47.全排列 II
  • bws-rs:Rust 编写的 S3 协议网关框架,支持灵活后端接入
  • VBA 运用LISTBOX插件,选择多个选项,并将选中的选项回车录入当前选中的单元格
  • 关于NUC+雷达+倍福组网交换机是否完全足够的问题(是否需要一个路由器)