- Notifications
You must be signed in to change notification settings - Fork2.4k
Adds solutions for problems 17 & 51 in Kotlin#1605
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
b3e34f1
add solution for 51.N-Queens in Kotlin
1fba09e
add solution for 17. Letter Combinations of a Phone Number in Kotlin
2343fa7
📜 Update README table (🛠️ from Github Actions)
Ahmad-A0552747a
Merge branch 'main' into main
technophilistc8b75a7
Delete 51-N-Queens.kt
technophilist4a00721
📜 Update README table (🛠️ from Github Actions)
Ahmad-A0ae28643
Rename 17-Letter-Combinations-of-a-Phone-Number.kt to 0017-letter-com…
a93aFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletionskotlin/0017-letter-combinations-of-a-phone-number.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package kotlin | ||
class Solution { | ||
fun letterCombinations(digits: String): List<String> { | ||
if (digits.isEmpty()) return emptyList() | ||
val resultantList = mutableListOf<String>() | ||
val stringBuilder = StringBuilder() | ||
val digitCharListMapping = mutableMapOf( | ||
'2' to listOf('a', 'b', 'c'), | ||
'3' to listOf('d', 'e', 'f'), | ||
'4' to listOf('g', 'h', 'i'), | ||
'5' to listOf('j', 'k', 'l'), | ||
'6' to listOf('m', 'n', 'o'), | ||
'7' to listOf('p', 'q', 'r', 's'), | ||
'8' to listOf('t', 'u', 'v'), | ||
'9' to listOf('w', 'x', 'y', 'z') | ||
) | ||
fun dfs(decisionIndex: Int = 0) { | ||
if (stringBuilder.length == digits.length) { | ||
resultantList.add(stringBuilder.toString()) | ||
return | ||
} | ||
val charListForDigitAtDecisionIndex = digitCharListMapping.getValue(digits[decisionIndex]) | ||
for (char in charListForDigitAtDecisionIndex) { | ||
stringBuilder.append(char) | ||
dfs(decisionIndex + 1) | ||
stringBuilder.deleteCharAt(stringBuilder.lastIndex) | ||
} | ||
} | ||
dfs() | ||
return resultantList | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.