java: add “1768. Merge Strings Alternately”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
b20b6b2d9a
commit
77096af698
1 changed files with 20 additions and 0 deletions
20
java/merge-strings-alternately.java
Normal file
20
java/merge-strings-alternately.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
class Solution {
|
||||||
|
public String mergeAlternately(String word1, String word2) {
|
||||||
|
StringBuilder sb = new StringBuilder(word1.length() + word2.length());
|
||||||
|
|
||||||
|
int i, j;
|
||||||
|
for (i = 0, j = 0; i < word1.length() && j < word2.length(); ++i, ++j) {
|
||||||
|
sb.append(word1.charAt(i));
|
||||||
|
sb.append(word2.charAt(j));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < word1.length()) {
|
||||||
|
sb.append(word1.substring(i));
|
||||||
|
}
|
||||||
|
if (j < word2.length()) {
|
||||||
|
sb.append(word2.substring(j));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue