-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0784-letter-case-permutation.js
More file actions
35 lines (33 loc) · 946 Bytes
/
0784-letter-case-permutation.js
File metadata and controls
35 lines (33 loc) · 946 Bytes
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
/**
* 784. Letter Case Permutation
* https://leetcode.com/problems/letter-case-permutation/
* Difficulty: Medium
*
* Given a string s, you can transform every letter individually to be lowercase or uppercase
* to create another string.
*
* Return a list of all possible strings we could create. Return the output in any order.
*/
/**
* @param {string} str
* @return {string[]}
*/
var letterCasePermutation = function(str) {
const result = [];
backtrack(result, str);
return result;
};
function backtrack(result, input, permutation = '', offset = 0) {
if (input.length === permutation.length) {
result.push(permutation);
} else {
const target = input[offset];
if (isNaN(target)) {
[target.toLowerCase(), target.toUpperCase()].forEach(s => {
backtrack(result, input, permutation + s, offset + 1);
});
} else {
backtrack(result, input, permutation + target, offset + 1);
}
}
}