Created
June 13, 2024 01:44
-
-
Save burkaslarry/b4dabc12de0b0d10cfbdc9447cdc673f to your computer and use it in GitHub Desktop.
Letter Combinations of a Phone Number - Kotlin
This file contains 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
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