通过在 JavaScript 中更改大小写来创建排列
问题
我们需要编写一个JavaScript函数,它接受一串字符串str作为第一个也是唯一的参数。
我们的函数可以将每个字母单独转换为小写或大写以创建另一个字符串。我们应该返回一个我们可以创建的所有可能字符串的列表。
例如,如果函数的输入是
输入
const str = 'k1l2';
输出
const output = ["k1l2","k1L2","K1l2","K1L2"];
示例
以下是代码-
const str = 'k1l2';
const changeCase = function (S = '') {
   const res = []
   const helper = (ind = 0, current = '') => {
      if (ind >= S.length) {
         res.push(current)
         return
      }
      if (/[a-zA-Z]/.test(S[ind])) {
         helper(ind + 1, current + S[ind].toLowerCase())
         helper(ind + 1, current + S[ind].toUpperCase())
      } else {
         helper(ind + 1, current + S[ind])
      }
   }
   helper()
   return res
};
console.log(changeCase(str));输出结果[ 'k1l2', 'k1L2', 'K1l2', 'K1L2' ]
