10000 add fix for 1768 (#175) · githubniraj/Leetcode@ac64645 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac64645

Browse files
authored
add fix for 1768 (fishercoder1534#175)
1 parent 0ec4e19 commit ac64645

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,22 @@ public String mergeAlternately(String word1, String word2) {
1919
return sb.toString();
2020
}
2121
}
22+
public static class Solution2 {
23+
public String mergeAlternately(String word1, String word2) {
24+
int len1 = word1.length();
25+
int len2 = word2.length();
26+
StringBuilder sb = new StringBuilder();
27+
28+
int diffLen = Math.min(len1, len2);
29+
int i = 0;
30+
for(i = 0; i < diffLen; i++) {
31+
sb.append(word1.charAt(i));
32+
sb.append(word2.charAt(i));
33+
}
34+
if (i >= len1) sb.append(word2.substring(i));
35+
else sb.append(word1.substring(i));
36+
37+
return sb.toString();
38+
}
39+
}
2240
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1768;
4+
import org.junit.Assert;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
9+
public class _1768Test {
10+
private static _1768.Solution2 solution2;
11+
private static String word1;
12+
private static String word2;
13+
private static String expected;
14+
private static String actual;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution2 = new _1768.Solution2();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
word1 = "abc";
24+
word2 = "pqr";
25+
expected = "apbqcr";
26+
actual = solution2.mergeAlternately(word1, word2);
27+
Assert.assertEquals(actual, expected);
28+
}
29+
}

0 commit comments

Comments
 (0)
0