Skip to content

Commit dfaae98

Browse files
add 784
1 parent ddf6445 commit dfaae98

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | O(n*2^n) | O(n*2^n) | |Easy|
2526
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | O(n) | O(h) | |Easy|
2627
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | O(logn) | O(1) | |Medium|
2728
|776|[Split BST](https://leetcode.com/problems/split-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | O(n) | O(n) | |Medium| Recursion
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
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+
31+
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());
60+
}
61+
}
62+
result.addAll(newResult);
63+
}
64+
}
65+
return new ArrayList<>(result);
66+
}
67+
}
68+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._784;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _784Test {
12+
private static _784.Solution1 solution1;
13+
private static List<String> expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _784.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
expected = Arrays.asList("a1b2", "a1B2", "A1b2", "A1B2");
23+
assertEquals(expected, solution1.letterCasePermutation("a1b2"));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
expected = Arrays.asList("3z4", "3Z4");
29+
assertEquals(expected, solution1.letterCasePermutation("3z4"));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
expected = Arrays.asList("12345");
35+
assertEquals(expected, solution1.letterCasePermutation("12345"));
36+
}
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy