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

Commit a9cb0b9

Browse files
add 1768
1 parent ac64645 commit a9cb0b9

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff 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
}

src/test/java/com/fishercoder/_1768Test.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.junit.BeforeClass;
66
import org.junit.Test;
77

8-
98
public 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
}

0 commit comments

Comments
 (0)