Leetcode: 1768. Merge Strings Alternately

Priyank SevakPriyank Sevak
2 min read

Problem Statement 1768. Merge Strings Alternately

Given two strings, word1 and word2, the task is to merge them by alternating characters. The process begins with word1 and continues until one string is exhausted. Any remaining characters from the longer string are appended to the end of the merged string.

My Thought Process

Given the problem's simplicity, I immediately recognized a two-pointer approach as the most suitable solution. My initial pseudocode outlined the following steps:

1.Initialize two pointers, one for each string. 2.Iterate through both strings, alternatingly adding characters to a new string until one string is empty. 3.Append the remaining characters from the non-empty string to the new string.

What Failed/Succeeded

To my satisfaction, this approach passed all test cases. The two-pointer strategy effectively handled the merging process and the subsequent appending of remaining characters.

Image description

Improvements

While the initial solution worked, I identified a potential optimization. Instead of maintaining two separate pointers, I could iterate based on the maximum length of the two strings. By checking if the current index is within the bounds of each string, I can directly append characters without unnecessary checks. This streamlined approach improves efficiency.

Time and Space Complexity

Time complexity: O(m + n), where m and n are the lengths of word1 and word2, respectively. This is because we iterate through each character in both strings once. Space complexity: O(m + n) as well, since we create a new string to store the merged result.

0
Subscribe to my newsletter

Read articles from Priyank Sevak directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priyank Sevak
Priyank Sevak

I'm a passionate software engineer with a strong foundation in both Salesforce development and traditional programming languages. My journey began with a Master's in Information Technology, and I've since honed my skills through diverse experiences, from game development to IT support. Currently, I'm a Software Engineer at Foundever, where I leverage Salesforce to build robust applications that streamline business processes. Prior to this, I played a key role in developing a custom Asset Management Tool for the company. My love for coding extends beyond Salesforce. I enjoy building web applications with technologies like React.js and Node.js, and I'm constantly exploring new ways to push the boundaries of my programming knowledge. Here's what I bring to the table: Salesforce Expertise: Adept in building robust Apex classes, triggers, Visualforce/Lightning pages, and more. Full-Stack Development: Proficient in JavaScript, Python, C#, Java, and web development frameworks like React.js. Problem-Solving Prowess: Proven ability to identify and resolve complex technical issues, both as a developer and IT support specialist. Communication & Collaboration: Excellent communication skills honed through collaborating with stakeholders and resolving client needs.