Skip to content

Commit 6408f2c

Browse files
committed
add 784 script.
1 parent 8934bd8 commit 6408f2c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

784-letter-case-permutation.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} S
3+
* @return {string[]}
4+
*/
5+
const letterCasePermutation = function(S) {
6+
let res = []
7+
backtrack(res, "", 0, S)
8+
return res
9+
};
10+
11+
const backtrack = (res, sol, depth, S) => {
12+
if (depth == S.length) {
13+
res.push(sol)
14+
return
15+
}
16+
const c = S[depth]
17+
if ("1234567890".indexOf(c) != - 1) {
18+
backtrack(res, sol+c, depth + 1, S)
19+
} else {
20+
backtrack(res, sol+c.toLowerCase(), depth + 1, S)
21+
backtrack(res, sol+c.toUpperCase(), depth + 1, S)
22+
}
23+
}
24+
25+
console.log(letterCasePermutation("a1b2"))
26+
console.log(letterCasePermutation("3z4"))
27+
console.log(letterCasePermutation("12345"))

0 commit comments

Comments
 (0)