Skip to content

Add fix for problem 1768 #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1768.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ public String mergeAlternately(String word1, String word2) {
return sb.toString();
}
}
public static class Solution2 {
public String mergeAlternately(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
StringBuilder sb = new StringBuilder();

int diffLen = Math.min(len1, len2);
int i = 0;
for(i = 0; i < diffLen; i++) {
sb.append(word1.charAt(i));
sb.append(word2.charAt(i));
}
if (i >= len1) sb.append(word2.substring(i));
else sb.append(word1.substring(i));

return sb.toString();
}
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/fishercoder/_1768Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fishercoder;

import com.fishercoder.solutions._1768;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;


public class _1768Test {
private static _1768.Solution2 solution2;
private static String word1;
private static String word2;
private static String expected;
private static String actual;

@BeforeClass
public static void setup() {
solution2 = new _1768.Solution2();
}

@Test
public void test1() {
word1 = "abc";
word2 = "pqr";
expected = "apbqcr";
actual = solution2.mergeAlternately(word1, word2);
Assert.assertEquals(actual, expected);
}
}