Skip to content

Commit b8d7f5c

Browse files
committed
Add solution #784
1 parent 105a0fa commit b8d7f5c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
722|[Remove Comments](./0722-remove-comments.js)|Medium|
145145
733|[Flood Fill](./0733-flood-fill.js)|Easy|
146146
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
147+
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
147148
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
148149
796|[Rotate String](./0796-rotate-string.js)|Easy|
149150
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 784. Letter Case Permutation
3+
* https://leetcode.com/problems/letter-case-permutation/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, you can transform every letter individually to be lowercase or uppercase
7+
* to create another string.
8+
*
9+
* Return a list of all possible strings we could create. Return the output in any order.
10+
*/
11+
12+
/**
13+
* @param {string} str
14+
* @return {string[]}
15+
*/
16+
var letterCasePermutation = function(str) {
17+
const result = [];
18+
backtrack(result, str);
19+
return result;
20+
};
21+
22+
function backtrack(result, input, permutation = '', offset = 0) {
23+
if (input.length === permutation.length) {
24+
result.push(permutation);
25+
} else {
26+
const target = input[offset];
27+
if (isNaN(target)) {
28+
[target.toLowerCase(), target.toUpperCase()].forEach(s => {
29+
backtrack(result, input, permutation + s, offset + 1);
30+
});
31+
} else {
32+
backtrack(result, input, permutation + target, offset + 1);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)