-
Notifications
You must be signed in to change notification settings - Fork 154
/
letter-case-permutation.js
45 lines (40 loc) · 985 Bytes
/
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
45
/**
* Letter Case Permutation
*
* Given a string S, we can transform every letter individually to be
* lowercase or uppercase to create another string. Return a list of all possible strings we could create.
*
* Examples:
* Input: S = "a1b2"
* Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
*
* Input: S = "3z4"
* Output: ["3z4", "3Z4"]
*
* Input: S = "12345"
* Output: ["12345"]
* Note:
*
* S will be a string with length at most 12.
* S will consist only of letters or digits.
*/
/**
* @param {string} S
* @return {string[]}
*/
const letterCasePermutation = S => {
const result = [];
backtracking(S, 0, '', result);
return result;
};
const backtracking = (S, i, solution, result) => {
if (i === S.length) {
result.push(solution);
return;
}
backtracking(S, i + 1, solution + S[i].toLowerCase(), result);
if (/[a-zA-Z]/.test(S[i])) {
backtracking(S, i + 1, solution + S[i].toUpperCase(), result);
}
};
export { letterCasePermutation };