Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 278e1c5

Browse files
update 784
1 parent 1bcd012 commit 278e1c5

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
compile 'junit:junit:4.13'
2828
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.0'
2929
testCompile group: 'junit', name: 'junit', version:'4.13'
30+
testCompile("org.assertj:assertj-core:3.11.1")
3031

3132
compileOnly 'org.projectlombok:lombok:1.18.12'
3233
annotationProcessor 'org.projectlombok:lombok:1.18.12'

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

+35
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,39 @@ public List<String> letterCasePermutation(String S) {
4242
return new ArrayList<>(result);
4343
}
4444
}
45+
46+
public static class Solution2 {
47+
/**
48+
* My completely original solution on 10/11/2021.
49+
*/
50+
public List<String> letterCasePermutation(String s) {
51+
List<String> ans = new ArrayList<>();
52+
ans.add("");
53+
return recursion(s, ans, 0);
54+
}
55+
56+
private List<String> recursion(String s, List<String> ans, int start) {
57+
if (start >= s.length()) {
58+
return ans;
59+
}
60+
List<String> newList = new ArrayList<>();
61+
int index = start;
62+
while (start < s.length() && Character.isDigit(s.charAt(start))) {
63+
start++;
64+
}
65+
String digits = "";
66+
if (start > index) {
67+
digits = s.substring(index, start);
68+
}
69+
for (String str : ans) {
70+
if (start < s.length()) {
71+
newList.add(str + digits + Character.toUpperCase(s.charAt(start)));
72+
newList.add(str + digits + Character.toLowerCase(s.charAt(start)));
73+
} else {
74+
newList.add(str + digits);
75+
}
76+
}
77+
return recursion(s, newList, start + 1);
78+
}
79+
}
4580
}
+33-26
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._784;
4+
45
import java.util.Arrays;
56
import java.util.List;
7+
68
import org.junit.BeforeClass;
79
import org.junit.Test;
810

9-
import static org.junit.Assert.assertEquals;
11+
import static org.assertj.core.api.Assertions.assertThat;
1012

1113
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-
}
14+
private static _784.Solution1 solution1;
15+
private static _784.Solution2 solution2;
16+
private static List<String> expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _784.Solution1();
21+
solution2 = new _784.Solution2();
22+
}
23+
24+
@Test
25+
public void test1() {
26+
expected = Arrays.asList("a1b2", "a1B2", "A1b2", "A1B2");
27+
assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("a1b2"));
28+
assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("a1b2"));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
expected = Arrays.asList("3z4", "3Z4");
34+
assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("3z4"));
35+
assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("3z4"));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
expected = Arrays.asList("12345");
41+
assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("12345"));
42+
assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("12345"));
43+
}
3744
}

0 commit comments

Comments
 (0)