Skip to content

Create Letter_Combinations_of_a_Phone_Number.cpp #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions backtracking_problems/Letter_Combinations_of_a_Phone_Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
private:
void sol(string digits, string output, int index, vector<string>& ans, string map[]){

// base case
if (index > digits.length()) return;

if (index == digits.length()) {
ans.push_back(output);
return;
}

// If we dont subtract 0, then output will be given as a char since digits is a string
// But we need number in int --> so subtract 0
int number = digits[index] - '0';
string value = map[number];

for (int i = 0; i < value.length(); i++){
output.push_back(value[i]);
sol(digits, output, index + 1, ans, map);

// backtracking the changes made
output.pop_back();
}

}

public:
vector<string> letterCombinations(string digits) {

vector <string> ans;
string output;
int index = 0;

// In case of empty string --> output will also be an empty arr
if (digits.length() == 0) return ans;

// Mapping index with corresponding strings
string map[10] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

sol(digits, output, index, ans, map);

return ans;
}

};