LeetCode in Kotlin

784. Letter Case Permutation

Medium

Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.

Return a list of all possible strings we could create. Return the output in any order.

Example 1:

Input: s = “a1b2”

Output: [“a1b2”,”a1B2”,”A1b2”,”A1B2”]

Example 2:

Input: s = “3z4”

Output: [“3z4”,”3Z4”]

Constraints:

Solution

import java.util.Locale

class Solution {
    private val ans: MutableList<String> = ArrayList()

    fun letterCasePermutation(s: String): List<String> {
        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()))
            }
        }
    }
}