-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCasePermutation.java
More file actions
30 lines (26 loc) · 896 Bytes
/
LetterCasePermutation.java
File metadata and controls
30 lines (26 loc) · 896 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
30
import java.util.*;
public class LetterCasePermutation {
public static List<String> getPermutations(String s) {
List<String> result = new ArrayList<>();
helper(s, 0, "", result);
return result;
}
private static void helper(String s, int index, String current, List<String> result) {
if (index == s.length()) {
result.add(current);
return;
}
char c = s.charAt(index);
if (Character.isLetter(c)) {
helper(s, index + 1, current + Character.toLowerCase(c), result);
helper(s, index + 1, current + Character.toUpperCase(c), result);
} else {
helper(s, index + 1, current + c, result);
}
}
public static void main(String[] args) {
String s = "a1b2";
List<String> res = getPermutations(s);
System.out.println(res);
}
}