Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save burkaslarry/b4dabc12de0b0d10cfbdc9447cdc673f to your computer and use it in GitHub Desktop.
Save burkaslarry/b4dabc12de0b0d10cfbdc9447cdc673f to your computer and use it in GitHub Desktop.
Letter Combinations of a Phone Number - Kotlin
class Solution {
private val vals = arrayOf("", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz")
private var result: MutableList<String> = mutableListOf()
fun letterCombinations(digits: String): List<String> {
if (digits.isEmpty()) return result
dfs(digits, 0, "")
return result
}
private fun dfs(digits: String, cur: Int, temp: String) {
if (cur == digits.length) {
result.add(temp)
return
}
val num = digits[cur].toInt() - '0'.toInt()
for (c in vals[num]) {
dfs(digits, cur + 1, temp + c)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment