-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
31 lines (28 loc) · 967 Bytes
/
Main.java
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
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String S = "a1";
letterCasePermutation(S);
}
public static List<String> letterCasePermutation(String S) {
List<String> res = new ArrayList<>();
dfs(0, S.toCharArray(), res);
return res;
}
public static void dfs(int index, char[] S, List<String> res){
if(index == S.length){
res.add(new String(S));
return;
}
if(Character.isLetter(S[index])){
S[index] = Character.isUpperCase(S[index]) ? Character.toLowerCase(S[index]) : Character.toUpperCase(S[index]);
dfs(index + 1, S, res);
S[index] = Character.isUpperCase(S[index]) ? Character.toLowerCase(S[index]) : Character.toUpperCase(S[index]);
dfs(index + 1, S, res);
}
else
dfs(index + 1, S, res);
}
}