Find my work at delgerskhn.vercel.app Contact me at delger.bayanmunkh6@gmail.com → Let’s build something 🛠️
Problem description
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.
Example 1:
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
Example 2:
Input: word1 = "ab", word2 = "pqrs" Output: "apbqrs" Explanation: Notice that as word2 is longer, "rs" is appended to the end. word1: a b word2: p q r s merged: a p b q r s
Example 3:
Input: word1 = "abcd", word2 = "pq" Output: "apbqcd" Explanation: Notice that as word1 is longer, "cd" is appended to the end. word1: a b c d word2: p q merged: a p b q c d
Constraints:
1 <= word1.length, word2.length <= 100
word1
andword2
consist of lowercase English letters.
Intuition
Since we are going to mix the two strings we need pointers for the strings. That seems easier than using one iterator.
If one of the pointers finished iterating, we simply append the other one at the end of the output string. It’s where that first if conditions in while loop is come from.
We must begin building the string from first word. Hence, the second pointer must be lower than first pointer. So, simply append the current char from word2 if pointer 2 is less than pointer 1 otherwise appending from word1 will suffice the algorithm.
Finally, don’t forget to return the output string at the end of the function body 😉.
Implementation
public class Solution { public string MergeAlternately(string word1, string word2) { int l1 = word1.Length; int l2 = word2.Length; int i1 = 0; int i2 = 0; string output = ""; while(i1 < l1 || i2 < l2) { if(i1 == l1 && i2 < l2) { output += word2[i2++]; continue; } if(i2 == l2 && i1 < l1) { output += word1[i1++]; continue; } if(i2 < i1) { output+=word2[i2++]; } else { output += word1[i1++]; } } return output; } }
Summary
It took some time to solve this problem as I haven’t been doing leetcode past few months. Maybe my code is lengthy, but it works fine. The breaking condition of while loop was kinda tricky that I got some index out of range errors when I put it like
i1 + i2 < l1 + l2 - 2
. Thus, I used the condition above to check both iterators are finished.I guess the reason I got 96% on runtime speed is maybe because there aren’t many submissions in C# 😅.
Blog Posts
Went on business trip to China
A Note on a business trip to China. Meeting several Chinese biggest refrigerator, freezer manufacturing companies
Jan 18, 2025
Business
Trip
Startups
Career
Projects
Thought Experiments
Tech
Creating a Barcode Detector Using the Browser BarcodeDetector API
In this tutorial, we'll create a simple barcode detector web application using the Browser BarcodeDetector API. The app will access the user's webcam, display the video stream, and detect barcodes in real-time.
Jun 17, 2024
Javascript
Computer Science
Video
Web Dev
Software Development
App Dev
Thought Experiments
Tech
Coding
Programming
26. Remove Duplicates from Sorted Array
Solving leetcode problem 26. Remove duplicates from sorted array using two pointer approach.
Apr 8, 2024
C#
Leetcode
Coding
Competitive programming
Problem solving
27. Remove Element
Leetcode (Remove Element) problem solution using two pointers.
Apr 8, 2024
Leetcode
Coding
Problem solving
Competitive programming
C#
Computer Science
Building your portfolio website backed by Notion as CMS
Publish your portfolio notion page to the web under 30 minutes using Next.js.
Apr 2, 2024
Node.js
Next.js
Problem solving
Blog
Lifestyle
Programming
Web Dev
45. Jump Game II
Solve leetcode 45. Jump Game II using greedy approach.
Apr 15, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
Greedy Algorithm
122. Best Time to Buy and Sell Stock II
Solve leetcode 122. Best Time to Buy and Sell Stock II using greedy algorithm
Apr 10, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
121. Best Time to Buy and Sell Stock
Solve leetcode 121. Best Time to Buy and Sell Stock using simple greedy algorithm.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
169. Majority Element
Solve leetcode 169. Majority Element with sorting method.
Apr 9, 2024
C#
Leetcode
Coding
Programming
Problem solving
Competitive programming
80. Remove Duplicates from Sorted Array II
Intuition and algorithm to solve 80. Remove Duplicates from Sorted Array II
Apr 3, 2024
Leetcode
Coding
Problem solving
C#
Empowering legacy KonvaJS in React projects
Building visual editor tools with Konva and React.
Coding
Problem solving
Creativity
Library
Node.js
React.js
Konva.js