/* Pseudocode: Merge Strings Alternately
FUNCTION mergeStringsAlternately(string1, string2)
INITIALIZE result as an empty string
INITIALIZE i = 0
INITIALIZE j = 0
WHILE i < length(string1) OR j < length(string2)
IF i < length(string1)
APPEND string1[i] to result
INCREMENT i
ENDIF
IF j < length(string2)
APPEND string2[j] to result
INCREMENT j
ENDIF
ENDWHILE
RETURN result
END FUNCTION
*/
class Solution {
public static void main(String[] args) {
String word1 = "abc";
String word2 = "pqr";
Solution solution = new Solution();
System.out.println(solution.mergeAlternately(word1, word2));
}
public String mergeAlternately(String word1, String word2) {
StringBuilder result = new StringBuilder();
int i = 0, j = 0;
int len1 = word1.length(), len2 = word2.length();
while (i < len1 || j < len2) {
if (i < len1) {
result.append(word1.charAt(i));
i++;
}
if (j < len1) {
result.append(word2.charAt(j));
j++;
}
}
return result.toString();
}
}