-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringPermutationsII.java
More file actions
37 lines (32 loc) · 1.51 KB
/
StringPermutationsII.java
File metadata and controls
37 lines (32 loc) · 1.51 KB
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
32
33
34
35
36
37
import java.util.*;
class StringPermutationsII {
public static List<String> findLetterCaseStringPermutations(String str) {
List<String> permutations = new ArrayList<>();
if (str == null)
return permutations;
permutations.add(str);
// process every character of the string one by one
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) { // only process characters, skip digits
// we will take all existing permutations and change the letter case appropriately
int n = permutations.size();
for (int j = 0; j < n; j++) {
char[] chs = permutations.get(j).toCharArray();
// if the current character is in upper case change it to lower case or vice versa
if (Character.isUpperCase(chs[i]))
chs[i] = Character.toLowerCase(chs[i]);
else
chs[i] = Character.toUpperCase(chs[i]);
permutations.add(String.valueOf(chs));
}
}
}
return permutations;
}
public static void main(String[] args) {
List<String> result = StringPermutationsII.findLetterCaseStringPermutations("ad52");
System.out.println(" String permutations are: " + result);
result = StringPermutationsII.findLetterCaseStringPermutations("ab7c");
System.out.println(" String permutations are: " + result);
}
}