-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0784-letter-case-permutation.js
More file actions
56 lines (49 loc) · 1.46 KB
/
0784-letter-case-permutation.js
File metadata and controls
56 lines (49 loc) · 1.46 KB
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
46
47
48
49
50
51
52
53
54
55
56
/**
* Letter Case Permutation
* Time Complexity: O(N * 2^N)
* Space Complexity: O(N * 2^N)
*/
var letterCasePermutation = function (s) {
const allPermutations = [];
function generatePermutations(
collectedStrings,
originalTextSource,
currentPermutationBuild,
processingPositionIndex,
) {
if (processingPositionIndex === originalTextSource.length) {
collectedStrings.push(currentPermutationBuild);
return;
}
const charToExamine = originalTextSource[processingPositionIndex];
const nextPositionIncrement = processingPositionIndex + 1;
const isCharLetter =
(charToExamine >= "a" && charToExamine <= "z") ||
(charToExamine >= "A" && charToExamine <= "Z");
if (isCharLetter) {
const lowerCasedVariant = charToExamine.toLowerCase();
generatePermutations(
collectedStrings,
originalTextSource,
currentPermutationBuild + lowerCasedVariant,
nextPositionIncrement,
);
const upperCasedVariant = charToExamine.toUpperCase();
generatePermutations(
collectedStrings,
originalTextSource,
currentPermutationBuild + upperCasedVariant,
nextPositionIncrement,
);
} else {
generatePermutations(
collectedStrings,
originalTextSource,
currentPermutationBuild + charToExamine,
nextPositionIncrement,
);
}
}
generatePermutations(allPermutations, s, "", 0);
return allPermutations;
};