File tree Expand file tree Collapse file tree 2 files changed +12
-7
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 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) {
1919 return sb .toString ();
2020 }
2121 }
22+
2223 public static class Solution2 {
2324 public String mergeAlternately (String word1 , String word2 ) {
2425 int len1 = word1 .length ();
2526 int len2 = word2 .length ();
2627 StringBuilder sb = new StringBuilder ();
27-
2828 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 ++) {
3131 sb .append (word1 .charAt (i ));
3232 sb .append (word2 .charAt (i ));
3333 }
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+ }
3739 return sb .toString ();
3840 }
3941 }
Original file line number Diff line number Diff line change 55import org .junit .BeforeClass ;
66import org .junit .Test ;
77
8-
98public class _1768Test {
9+ private static _1768 .Solution1 solution1 ;
1010 private static _1768 .Solution2 solution2 ;
1111 private static String word1 ;
1212 private static String word2 ;
@@ -15,6 +15,7 @@ public class _1768Test {
1515
1616 @ BeforeClass
1717 public static void setup () {
18+ solution1 = new _1768 .Solution1 ();
1819 solution2 = new _1768 .Solution2 ();
1920 }
2021
@@ -23,6 +24,8 @@ public void test1() {
2324 word1 = "abc" ;
2425 word2 = "pqr" ;
2526 expected = "apbqcr" ;
27+ actual = solution1 .mergeAlternately (word1 , word2 );
28+ Assert .assertEquals (actual , expected );
2629 actual = solution2 .mergeAlternately (word1 , word2 );
2730 Assert .assertEquals (actual , expected );
2831 }
You can’t perform that action at this time.
0 commit comments