forked from CS3704-VT/tech-interview-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution
More file actions
38 lines (36 loc) · 974 Bytes
/
solution
File metadata and controls
38 lines (36 loc) · 974 Bytes
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
34
35
36
37
38
class Solution {
public String mergeAlternately(String word1, String word2)
{
char[] arr1 = word1.toCharArray();
char[] arr2 = word2.toCharArray();
int len1 = word1.length();
int len2 = word2.length();
char[] retArr = new char[len1 + len2];
if (len1 > len2)
{
for (int i = 0; i < len2; i++)
{
retArr[i] = arr1[i];
retArr[i + 1] = arr2[i];
}
for (int i = len2; i < len1; i++)
{
retArr[i] = arr1[i];
}
return retArr.toString();
}
else
{
for (int i = 0; i < len1; i++)
{
retArr[i] = arr1[i];
retArr[i + 1] = arr2[i];
}
for (int i = len1; i < len2; i++)
{
retArr[i] = arr2[i];
}
return retArr.toString();
}
}
}