![JAR search and dependency download from the Maven repository](/logo.png)
g0701_0800.s0784_letter_case_permutation.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0784_letter_case_permutation
// #Medium #String #Bit_Manipulation #Backtracking #Algorithm_I_Day_11_Recursion_Backtracking
// #2023_03_12_Time_219_ms_(84.62%)_Space_37.7_MB_(64.10%)
import java.util.Locale
class Solution {
private val ans: MutableList = ArrayList()
fun letterCasePermutation(s: String): List {
helper(s, 0, "")
return ans
}
private fun helper(s: String, curr: Int, temp: String) {
if (curr == s.length) {
ans.add(temp)
return
}
if (Character.isDigit(s[curr])) {
helper(s, curr + 1, temp + s[curr])
} else {
if (Character.isLowerCase(s[curr])) {
helper(s, curr + 1, temp + s[curr])
helper(s, curr + 1, temp + s.substring(curr, curr + 1).uppercase(Locale.getDefault()))
} else {
helper(s, curr + 1, temp + s[curr])
helper(s, curr + 1, temp + s.substring(curr, curr + 1).lowercase(Locale.getDefault()))
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy