code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

02 Letter Case Permutation

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const letterCasePermutation = val => {
  const result = []
  const backtrack = (index, combination) => {
    if (index === val.length) {
      result.push(combination)
      return
    }
    backtrack(index + 1, combination + val[index])
    if (val.charCodeAt(index) >= 'a'.charCodeAt(0) && val.charCodeAt(index) <= 'z'.charCodeAt(0)) {
      backtrack(index + 1, combination + val[index].toUpperCase())
    }
    if (val.charCodeAt(index) >= 'A'.charCodeAt(0) && val.charCodeAt(index) <= 'Z'.charCodeAt(0)) {
      backtrack(index + 1, combination + val[index].toLowerCase())
    }
  }
  backtrack(0, '')
  return result
}

console.log(letterCasePermutation('a1b2'))

Created 2022-02-25T01:16:46+00:00 · Edit