-
Notifications
You must be signed in to change notification settings - Fork 65
/
784.letter-case-permutation.js
44 lines (42 loc) · 1.13 KB
/
784.letter-case-permutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* @lc app=leetcode.cn id=784 lang=javascript
*
* [784] Letter Case Permutation
*/
// @lc code=start
/**
* @param {string} s
* @return {string[]}
* 'A'.charCodeAt() // 65
* 'a'.charCodeAt() // 97
* 'z'.charCodeAt() // 122
* 'Z'.charCodeAt() // 90
*/
var letterCasePermutation = function(s) {
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) {
// a-z 97-122 A-Z 65-90
let code = str.charCodeAt();
return (code >= 97 && code <= 122) || (code >= 65 && code <= 90);
}
// @lc code=end