Array / String· 15 min
Merge Strings Alternately
LeetCode #1768 · Easy · Interactive step-by-step visualization
Setup
Example input
word1 = "abc", word2 = "pqrstu"Step 1 / 20
Current step
1 / 20Place one cursor at the start of each string. The output is initially empty.
Step log
word1
Cursor
a
0b
1c
2word2
Cursor
p
0q
1r
2s
3t
4u
5result
No values to display.
Active
Comparing
Committed
Rejected or blocked
merge-strings-alternately.java
1public String mergeAlternately(String word1, String word2) {2 StringBuilder result = new StringBuilder();3 for (int i = 0; i < Math.max(word1.length(), word2.length()); i++) {4 if (i < word1.length()) result.append(word1.charAt(i));5 if (i < word2.length()) result.append(word2.charAt(i));6 }7 return result.toString();8}● Java
Time
O(n + m)
Space
O(n + m)
Pattern
two pointers
Frames
20
