code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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