Skip to main content

Command Palette

Search for a command to run...

Merge Strings Alternately

Published
2 min read
Merge Strings Alternately
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem: Link | Click Here

class Solution {
    public String mergeAlternately(String word1, String word2) {
        // Get lengths of both words
        int word1Length = word1.length();
        int word2Length = word2.length();

        // StringBuilder to build the merged string
        StringBuilder sb = new StringBuilder();

        // Determine the loop limit by considering the combined lengths of both words
        int loopLimit = word2Length + word1Length;

        // Initialize indices for both words
        int word1Index = 0;
        int word2Index = 0;

        // Iterate through the words to merge them alternately
        for (int i = 0; i < loopLimit; i++) {
            // If word2 is longer or both words have equal lengths
            if ((word2Length > word1Length) || (word1Length == word2Length)) {
                // Check for even index to append characters from word1
                if ((i % 2 == 0) && (word1Index < word1Length)) {
                    sb.append(word1.charAt(word1Index));
                    word1Index++;
                } else {
                    // Append characters from word2 if not an even index or word1 is finished
                    sb.append(word2.charAt(word2Index));
                    word2Index++;
                }
            }
            // If word1 is longer than word2
            if (word1Length > word2Length) {
                // Check for odd index to append characters from word2
                if ((i % 2 != 0) && (word2Index < word2Length)) {
                    sb.append(word2.charAt(word2Index));
                    word2Index++;
                } else {
                    // Append characters from word1 if not an odd index or word2 is finished
                    sb.append(word1.charAt(word1Index));
                    word1Index++;
                }
            }
        }

        // Convert the StringBuilder to a string
        String newString = sb.toString();

        // Return the merged string
        return newString;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.