-
Notifications
You must be signed in to change notification settings - Fork 0
/
1768. Merge Strings Alternately.java
48 lines (31 loc) · 1.44 KB
/
1768. Merge Strings Alternately.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
1768. Merge Strings Alternately
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
Constraints:
1 <= word1.length, word2.length <= 100
word1 and word2 consist of lowercase English letters.
*/
class Solution {
public String mergeAlternately(String word1, String word2) {
String [] word1_tokens = word1.split("");
String [] word2_tokens = word2.split("");
StringBuilder mergedString = new StringBuilder();
String [] word3_tokens = new String [word1_tokens.length + word2_tokens.length];
int minLength = Math.min(word1_tokens.length, word2_tokens.length);
for(int i = 0; i < minLength; i++){
mergedString.append(word1_tokens[i]);
mergedString.append(word2_tokens[i]);
word1_tokens[i] = "";
word2_tokens[i] = "";
}
mergedString.append( String.join("", word1_tokens)).append(String.join("", word2_tokens));
return mergedString.toString();
}
}