Skip to content

784. Letter Case Permutation

https://leetcode.com/problems/letter-case-permutation/

js
/**
 * @param {string} S
 * @return {string[]}
 */
var letterCasePermutation = function(S) {
  let result = ['']
  S.split('').forEach(c => {
    if (c >= 'A' && c <= 'z') {
      result = result.reduce((ret, p) => {
        ret.push(p + c.toLowerCase(), p + c.toUpperCase())
        return ret
      }, [])
    } else {
      result = result.map(p => p + c)
    }
  })
  return result
}
py
class Solution:

    def letterCasePermutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        result = ['']
        for c in S:
            if c.isalpha():
                result = [p + c.lower() for p in result] + \
                    [p + c.upper() for p in result]
            else:
                result = [p + c for p in result]
        return result