File tree 2 files changed +12
-7
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -19,21 +19,23 @@ public String mergeAlternately(String word1, String word2) {
19
19
return sb .toString ();
20
20
}
21
21
}
22
+
22
23
public static class Solution2 {
23
24
public String mergeAlternately (String word1 , String word2 ) {
24
25
int len1 = word1 .length ();
25
26
int len2 = word2 .length ();
26
27
StringBuilder sb = new StringBuilder ();
27
-
28
28
int diffLen = Math .min (len1 , len2 );
29
- int i = 0 ;
30
- for (i = 0 ; i < diffLen ; i ++) {
29
+ int i ;
30
+ for (i = 0 ; i < diffLen ; i ++) {
31
31
sb .append (word1 .charAt (i ));
32
32
sb .append (word2 .charAt (i ));
33
33
}
34
- if (i >= len1 ) sb .append (word2 .substring (i ));
35
- else sb .append (word1 .substring (i ));
36
-
34
+ if (i >= len1 ) {
35
+ sb .append (word2 .substring (i ));
36
+ } else {
37
+ sb .append (word1 .substring (i ));
38
+ }
37
39
return sb .toString ();
38
40
}
39
41
}
Original file line number Diff line number Diff line change 5
5
import org .junit .BeforeClass ;
6
6
import org .junit .Test ;
7
7
8
-
9
8
public class _1768Test {
9
+ private static _1768 .Solution1 solution1 ;
10
10
private static _1768 .Solution2 solution2 ;
11
11
private static String word1 ;
12
12
private static String word2 ;
@@ -15,6 +15,7 @@ public class _1768Test {
15
15
16
16
@ BeforeClass
17
17
public static void setup () {
18
+ solution1 = new _1768 .Solution1 ();
18
19
solution2 = new _1768 .Solution2 ();
19
20
}
20
21
@@ -23,6 +24,8 @@ public void test1() {
23
24
word1 = "abc" ;
24
25
word2 = "pqr" ;
25
26
expected = "apbqcr" ;
27
+ actual = solution1 .mergeAlternately (word1 , word2 );
28
+ Assert .assertEquals (actual , expected );
26
29
actual = solution2 .mergeAlternately (word1 , word2 );
27
30
Assert .assertEquals (actual , expected );
28
31
}
You can’t perform that action at this time.
0 commit comments