Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit278e1c5

Browse files
update 784
1 parent1bcd012 commit278e1c5

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

‎build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
compile'junit:junit:4.13'
2828
compilegroup:'org.apache.commons',name:'commons-collections4',version:'4.0'
2929
testCompilegroup:'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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,39 @@ public List<String> letterCasePermutation(String S) {
4242
returnnewArrayList<>(result);
4343
}
4444
}
45+
46+
publicstaticclassSolution2 {
47+
/**
48+
* My completely original solution on 10/11/2021.
49+
*/
50+
publicList<String>letterCasePermutation(Strings) {
51+
List<String>ans =newArrayList<>();
52+
ans.add("");
53+
returnrecursion(s,ans,0);
54+
}
55+
56+
privateList<String>recursion(Strings,List<String>ans,intstart) {
57+
if (start >=s.length()) {
58+
returnans;
59+
}
60+
List<String>newList =newArrayList<>();
61+
intindex =start;
62+
while (start <s.length() &&Character.isDigit(s.charAt(start))) {
63+
start++;
64+
}
65+
Stringdigits ="";
66+
if (start >index) {
67+
digits =s.substring(index,start);
68+
}
69+
for (Stringstr :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+
returnrecursion(s,newList,start +1);
78+
}
79+
}
4580
}
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.solutions._784;
4+
45
importjava.util.Arrays;
56
importjava.util.List;
7+
68
importorg.junit.BeforeClass;
79
importorg.junit.Test;
810

9-
importstaticorg.junit.Assert.assertEquals;
11+
importstaticorg.assertj.core.api.Assertions.assertThat;
1012

1113
publicclass_784Test {
12-
privatestatic_784.Solution1solution1;
13-
privatestaticList<String>expected;
14-
15-
@BeforeClass
16-
publicstaticvoidsetup() {
17-
solution1 =new_784.Solution1();
18-
}
19-
20-
@Test
21-
publicvoidtest1() {
22-
expected =Arrays.asList("a1b2","a1B2","A1b2","A1B2");
23-
assertEquals(expected,solution1.letterCasePermutation("a1b2"));
24-
}
25-
26-
@Test
27-
publicvoidtest2() {
28-
expected =Arrays.asList("3z4","3Z4");
29-
assertEquals(expected,solution1.letterCasePermutation("3z4"));
30-
}
31-
32-
@Test
33-
publicvoidtest3() {
34-
expected =Arrays.asList("12345");
35-
assertEquals(expected,solution1.letterCasePermutation("12345"));
36-
}
14+
privatestatic_784.Solution1solution1;
15+
privatestatic_784.Solution2solution2;
16+
privatestaticList<String>expected;
17+
18+
@BeforeClass
19+
publicstaticvoidsetup() {
20+
solution1 =new_784.Solution1();
21+
solution2 =new_784.Solution2();
22+
}
23+
24+
@Test
25+
publicvoidtest1() {
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+
publicvoidtest2() {
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+
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp