Skip to content

Commit 546727b

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 784_Letter_Case_Permutation.java
1 parent 0c27cf7 commit 546727b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Subsets/.gitkeep

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public List<String> letterCasePermutation(String S) {
3+
if (S == null || S.length() == 0) {
4+
return Collections.emptyList();
5+
}
6+
7+
List<String> result = new ArrayList<>();
8+
dfs(S.toCharArray(), 0, result);
9+
return result;
10+
}
11+
12+
private void dfs(char[] word, int idx, List<String> result) {
13+
if (idx == word.length) {
14+
result.add(new String(word));
15+
return;
16+
}
17+
18+
if (Character.isDigit(word[idx])) {
19+
dfs(word, idx + 1, result);
20+
return;
21+
}
22+
23+
word[idx] = Character.toLowerCase(word[idx]);
24+
dfs(word, idx + 1, result);
25+
26+
word[idx] = Character.toUpperCase(word[idx]);
27+
dfs(word, idx + 1, result);
28+
}
29+
}

0 commit comments

Comments
 (0)