Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leetcode 784. 字母大小写全排列 #回溯 #39

Open
webVueBlog opened this issue May 29, 2022 · 1 comment
Open

leetcode 784. 字母大小写全排列 #回溯 #39

webVueBlog opened this issue May 29, 2022 · 1 comment

Comments

@webVueBlog
Copy link
Member

image
784. 字母大小写全排列

@webVueBlog
Copy link
Member Author

784. 字母大小写全排列

/*
 * @lc app=leetcode.cn id=784 lang=javascript
 *
 * [784] 字母大小写全排列
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string[]}给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。

返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。

'a'.charCodeAt() // 97

'z'.charCodeAt() // 122

A-Z 65-90
回溯 递归

(80 ms)
 */
var letterCasePermutation = function (s) {
  // current '' index是按照字符串索引排序,没有跳过等情况, res[]
  const dfs = (current, index, res) => {
    // a1 2 []
    if (current.length === s.length) {
      // 是否满足要求的一种答案
      res.push(current)
    }
    if (index >= s.length) {
      // 终止条件
      return
    }
    // 拿到当前值
    let str = s[index]
    if (isLetter(str)) {
      let lower = str.toLowerCase() // b
      let upper = str.toUpperCase() // B
      dfs(current + lower, index + 1, res) // a1b 3
      dfs(current + upper, index + 1, res) // a1B 3
    } else {
      dfs(current + str, index + 1, res) // a1b2 a1B2
    }
  }
  let ans = []
  dfs('', 0, ans)
  return ans
}

function isLetter(str) {
  let code = str.charCodeAt()
  return (code >= 97 && code <= 122) || (code >= 65 && code <= 90)
}
// @lc code=end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant