-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
algorithm: letter combinations #1209
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
algorithm: letter combinations #1209
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this a backtracking problem? At first glance this appears to be a simple combinatorial problem: Finding the cross product of the sets of possible letters for all numbers.
@appgurueu Sir you are correct when there are just two digits in the provided string, but if we analyze the scenario of three digits, we notice that we need to backtrack for the second digit string. |
I wrote the following solution which got accepted in Go: func letterCombinations(digits string) []string {
if digits == "" {
return []string{}
}
letters := [8]string{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
res := []string{""}
for _, digit := range digits {
nextRes := []string{}
for _, letter := range letters[digit - '2'] {
for _, str := range res {
nextRes = append(nextRes, str + string(letter))
}
}
res = nextRes
}
return res
} this problem does not inherently require recursion or backtracking? As said, it's just the cartesian product |
Okay Sir, I guess your solution is iterative and mine is recursive https://www.interviewbit.com/blog/letter-combinations-of-a-phone-number/ |
- from https://en.wikipedia.org/wiki/Backtracking I don't see how this definition is met - no "candidates" are "abandoned" (if I'm missing something, please explain). All I see is a simple combinatorial algorithm. LeetCode and InterviewBit are probably getting their terminology wrong. @raklaptudirm your opinion? Should this go under "Backtracking" or just "Recursion"? In the Lua repo I prefer to categorize algorithms not by the applied "strategy", but rather strictly by the problem they solve. |
Please review @raklaptudirm |
I guess recursive would be best. |
Okay I will update |
Done @appgurueu @raklaptudirm |
It is one of the most common Backtracking Problems asked during interviews
Describe your change:
Checklist:
Example:
UserProfile.js
is allowed butuserprofile.js
,Userprofile.js
,user-Profile.js
,userProfile.js
are notFixes: #{$ISSUE_NO}
.