Skip to content

Commit 68accd1

Browse files
Steve SunSteve Sun
authored andcommitted
update 1769
1 parent e8ada23 commit 68accd1

2 files changed

Lines changed: 4 additions & 30 deletions

File tree

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,14 @@ public static class Solution1 {
55
public String mergeAlternately(String word1, String word2) {
66
StringBuilder sb = new StringBuilder();
77
int i = 0;
8-
int j = 0;
9-
for (; i < word1.length() && j < word2.length(); ) {
10-
sb.append(word1.charAt(i++));
11-
sb.append(word2.charAt(j++));
12-
}
13-
while (i < word1.length()) {
14-
sb.append(word1.charAt(i++));
15-
}
16-
while (j < word2.length()) {
17-
sb.append(word2.charAt(j++));
18-
}
19-
return sb.toString();
20-
}
21-
}
22-
23-
public static class Solution2 {
24-
public String mergeAlternately(String word1, String word2) {
25-
int len1 = word1.length();
26-
int len2 = word2.length();
27-
StringBuilder sb = new StringBuilder();
28-
int diffLen = Math.min(len1, len2);
29-
int i;
30-
for (i = 0; i < diffLen; i++) {
8+
for (; i < word1.length() && i < word2.length(); i++) {
319
sb.append(word1.charAt(i));
3210
sb.append(word2.charAt(i));
3311
}
34-
if (i >= len1) {
35-
sb.append(word2.substring(i));
36-
} else {
12+
if (i < word1.length()) {
3713
sb.append(word1.substring(i));
14+
} else if (i < word2.length()) {
15+
sb.append(word2.substring(i));
3816
}
3917
return sb.toString();
4018
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
public class _1768Test {
99
private _1768.Solution1 solution1;
10-
private _1768.Solution2 solution2;
1110
private static String word1;
1211
private static String word2;
1312
private static String expected;
@@ -16,7 +15,6 @@ public class _1768Test {
1615
@BeforeEach
1716
public void setup() {
1817
solution1 = new _1768.Solution1();
19-
solution2 = new _1768.Solution2();
2018
}
2119

2220
@Test
@@ -26,7 +24,5 @@ public void test1() {
2624
expected = "apbqcr";
2725
actual = solution1.mergeAlternately(word1, word2);
2826
Assertions.assertEquals(actual, expected);
29-
actual = solution2.mergeAlternately(word1, word2);
30-
Assertions.assertEquals(actual, expected);
3127
}
3228
}

0 commit comments

Comments
 (0)