-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeStringsAlternately.java
33 lines (28 loc) · 1.02 KB
/
MergeStringsAlternately.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/merge-strings-alternately/
public class MergeStringsAlternately {
private final String firstString;
private final String secondString;
public MergeStringsAlternately(String firstString, String secondString) {
this.firstString = firstString;
this.secondString = secondString;
}
public String solution() {
char[] result = new char[firstString.length() + secondString.length()];
int firstPointer = 0;
int secondPointer = 0;
for (int i = 0; i < result.length; ) {
if (firstString.length() > firstPointer) {
result[i] = firstString.charAt(firstPointer);
firstPointer++;
i++;
}
if (secondString.length() > secondPointer) {
result[i] = secondString.charAt(secondPointer);
secondPointer++;
i++;
}
}
return String.valueOf(result);
}
}