Skip to content

Commit 29d3c24

Browse files
refactor 784
1 parent 7a1022d commit 29d3c24

File tree

1 file changed

+33
-56
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+33
-56
lines changed

src/main/java/com/fishercoder/solutions/_784.java

+33-56
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,41 @@
55
import java.util.List;
66
import java.util.Set;
77

8-
/**
9-
* 784. Letter Case Permutation
10-
*
11-
* Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.
12-
* Return a list of all possible strings we could create.
13-
14-
Examples:
15-
Input: S = "a1b2"
16-
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
17-
18-
Input: S = "3z4"
19-
Output: ["3z4", "3Z4"]
20-
21-
Input: S = "12345"
22-
Output: ["12345"]
23-
24-
Note:
25-
26-
S will be a string with length at most 12.
27-
S will consist only of letters or digits.
28-
29-
*/
30-
318
public class _784 {
32-
public static class Solution1 {
33-
public List<String> letterCasePermutation(String S) {
34-
Set<String> result = new HashSet<>();
35-
result.add(S);
36-
for (int i = 0; i < S.length(); i++) {
37-
if (Character.isAlphabetic(S.charAt(i))) {
38-
Set<String> newResult = new HashSet<>();
39-
for (String word : result) {
40-
if (Character.isUpperCase(word.charAt(i))) {
41-
StringBuilder sb = new StringBuilder();
42-
for (int j = 0; j < i; j++) {
43-
sb.append(word.charAt(j));
44-
}
45-
sb.append(Character.toLowerCase(word.charAt(i)));
46-
for (int j = i + 1; j < word.length(); j++) {
47-
sb.append(word.charAt(j));
48-
}
49-
newResult.add(sb.toString());
50-
} else {
51-
StringBuilder sb = new StringBuilder();
52-
for (int j = 0; j < i; j++) {
53-
sb.append(word.charAt(j));
54-
}
55-
sb.append(Character.toUpperCase(word.charAt(i)));
56-
for (int j = i + 1; j < word.length(); j++) {
57-
sb.append(word.charAt(j));
58-
}
59-
newResult.add(sb.toString());
9+
public static class Solution1 {
10+
public List<String> letterCasePermutation(String S) {
11+
Set<String> result = new HashSet<>();
12+
result.add(S);
13+
for (int i = 0; i < S.length(); i++) {
14+
if (Character.isAlphabetic(S.charAt(i))) {
15+
Set<String> newResult = new HashSet<>();
16+
for (String word : result) {
17+
if (Character.isUpperCase(word.charAt(i))) {
18+
StringBuilder sb = new StringBuilder();
19+
for (int j = 0; j < i; j++) {
20+
sb.append(word.charAt(j));
21+
}
22+
sb.append(Character.toLowerCase(word.charAt(i)));
23+
for (int j = i + 1; j < word.length(); j++) {
24+
sb.append(word.charAt(j));
25+
}
26+
newResult.add(sb.toString());
27+
} else {
28+
StringBuilder sb = new StringBuilder();
29+
for (int j = 0; j < i; j++) {
30+
sb.append(word.charAt(j));
31+
}
32+
sb.append(Character.toUpperCase(word.charAt(i)));
33+
for (int j = i + 1; j < word.length(); j++) {
34+
sb.append(word.charAt(j));
35+
}
36+
newResult.add(sb.toString());
37+
}
38+
}
39+
result.addAll(newResult);
40+
}
6041
}
61-
}
62-
result.addAll(newResult);
42+
return new ArrayList<>(result);
6343
}
64-
}
65-
return new ArrayList<>(result);
6644
}
67-
}
6845
}

0 commit comments

Comments
 (0)