forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCasePermutation.java
More file actions
29 lines (27 loc) · 987 Bytes
/
LetterCasePermutation.java
File metadata and controls
29 lines (27 loc) · 987 Bytes
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
package LeetCode;
import java.util.ArrayList;
import java.util.List;
public class LetterCasePermutation {
private static List<String> res=new ArrayList<>();
public static List<String> letterCasePermutation(String S) {
helper(S,"",0);
return res;
}
public static void helper(String S,String currentString,int startIndex){
if (currentString.length()==S.length()){
res.add(currentString);
}else {
final char currentChar=S.charAt(startIndex);
if(Character.isLetter(currentChar)){
helper(S,currentString+Character.toLowerCase(currentChar),startIndex+1);
helper(S,currentString+Character.toUpperCase(currentChar),startIndex+1);
}else {
helper(S,currentString+currentChar,startIndex+1);
}
}
}
public static void main(String[] args){
String S="a1b2";
System.out.println(letterCasePermutation(S));
}
}