Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Merge two strings in chunks of given size

  • Difficulty Level : Hard
  • Last Updated : 22 Sep, 2021

Given two strings ‘a’ and ‘b’ and a number k, our aim is to merge the strings into a string s such that it contains groups of k characters from the strings alternately in the final string.

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: a = "durability", 
      b = "essence
      k = 3
Output: duressabienclitey

Input: a = "determination"
      b = "stance"
      k = 3
Output: detstaermnceination

In the second example we chose groups of three characters from determination and stance alternately to make the string. But when the string stance was completely used then we added the remaining string of determination as it is.



The approach of this problem is to alternately add groups of characters of both the strings until one of the string is finished. Then we simply have to add the remaining portion of the leftover string to the answer as it is.  

C++




// C++ program to merge n number of strings
#include <bits/stdc++.h>
using namespace std;
 
// Function performing the calculations
void solve(string a, string b, int k)
{
    string s = "";
 
    // Length of string a
    int la = a.length();
 
    // Length f string b
    int lb = b.length();
    int l = la + lb;
 
    // Pointers for string
    // a and string b
    int indexa = 0, indexb = 0;
    while (l > 0) {
 
        // pa and pb denote the number
        // of characters of both
        // a and b extracted
        int pa = 0, pb = 0;
 
        // If entire substring of length
        // k can be extracted
        if (la - indexa >= k) {
 
            // Please refer below link for
            // details of library function
            s = s + a.substr(indexa, k);
            indexa = indexa + k;
            pa = k;
        }
 
        // If the remaining string is
        // of length less than k
        else if (la - indexa < k && la - indexa > 0) {
            s = s + a.substr(indexa, la - indexa);
            pa = la - indexa;
            indexa = la;
        }
 
        // If the string has been
        // traversed
        else if (indexa >= la)
            pa = 0;
 
        // If entire substring of
        // length k can be extracted
        if (lb - indexb >= k) {
            s = s + b.substr(indexb, k);
            pb = k;
            indexb = indexb + k;
        }
 
        // If the remaining string is of
        // length less than k
        else if (lb - indexb < k && lb - indexb > 0) {
            s = s + b.substr(indexb, lb - indexb);
            pb = lb - indexb;
            indexb = lb;
        }
 
        // If the string has been
        // traversed
        else if (indexb >= lb)
            pb = 0;
        l = l - pa - pb;
    }
    cout << s;
}
 
// Driver function
int main()
{
    string a = "determination", b = "stance";
    int k = 3;
    solve(a, b, k);
    return 0;
}

Java




// Java program to merge
// n number of strings
import java.io.*;
class msc {
    // Function performing calculations
    public static void solve(String a,
                             String b, int k)
    {
        String s = "";
 
        // Length of string a
        int la = a.length();
 
        // Length f string b
        int lb = b.length();
        int l = la + lb;
 
        // Pointers for string a and string b
        int indexa = 0, indexb = 0;
        while (l > 0) {
 
            // pa and pb denote the number
            // of characters of both
            // a and b extracted
            int pa = 0, pb = 0;
 
            // If entire substring of
            // length k can be extracted
            if (la - indexa >= k) {
                s = s + a.substring(indexa, indexa + k);
                indexa = indexa + k;
                pa = k;
            }
 
            // If the remaining string is
            // of length less than k
            else if (la - indexa < k && la - indexa > 0) {
                s = s + a.substring(indexa, la);
                pa = la - indexa;
                indexa = la;
            }
 
            // If the string has been
            // traversed
            else if (indexa >= la)
                pa = 0;
 
            // If entire substring of
            // length k can be extracted
            if (lb - indexb >= k) {
                s = s + b.substring(indexb, indexb + k);
                pb = k;
                indexb = indexb + k;
            }
 
            // If the remaining string
            // is of length less than k
            else if (lb - indexb < k && lb - indexb > 0) {
                s = s + b.substring(indexb, lb);
                pb = lb - indexb;
                indexb = lb;
            }
 
            // If the string has been
            // traversed
            else if (indexb >= lb)
                pb = 0;
            l = l - pa - pb;
        }
        System.out.println(s);
    }
 
    // Driver function
    public static void main(String args[])
        throws IOException
    {
        String a = "determination", b = "stance";
        int k = 3;
        solve(a, b, k);
    }
}

Python3




# Python3 program to merge n number of strings
 
# Function performing the calculations
def solve(a, b, k):
    s = ""
 
    # Length of string a
    la = len(a)
 
    # Length f string b
    lb = len(b)
    l = la + lb
 
    # Pointers for string
    # a and string b
    indexa = indexb = 0
     
    while l:
        # pa and pb denote the number
        # of characters of both
        # a and b extracted
        pa = pb = 0
 
        # If entire substring of length
        # k can be extracted
        if la - indexa >= k:
            s = s + a[indexa : indexa + k]
            indexa = indexa + k
            pa = k
 
        # If the remaining string is
        # of length less than k
        elif la - indexa < k and la - indexa:
            s = s + a[indexa : la]
            pa = la - indexa
            indexa = la
 
        # If the string has been
        # traversed
        elif indexa >= la:
            pa = 0
 
        # If entire substring of
        # length k can be extracted
        if lb - indexb >= k:
            s = s + b[indexb : indexb+k]
            pb = k
            indexb = indexb + k
 
        # If the remaining string is of
        # length less than k
        elif lb - indexb < k and lb - indexb:
            s = s + b[indexb : lb]
            pb = lb - indexb
            indexb = lb
 
        # If the string has been
        # traversed
        elif indexb >= lb:
            pb = 0
        l = l - pa - pb
         
    print(s)
 
 
# Driver function
a = "determination"; b = "stance"
k = 3
solve(a, b, k)
 
 
# This code is contributed by Ansu Kumari

C#




// C# program to merge
// n number of strings
using System;
 
class GFG
{
    // Function performing
    // the calculations
    static void solve(string a,
                      string b,
                      int k)
    {
        string s = "";
     
        // Length of string a
        int la = a.Length;
     
        // Length f string b
        int lb = b.Length;
        int l = la + lb;
     
        // Pointers for string
        // a and string b
        int indexa = 0, indexb = 0;
        while (l > 0)
        {
     
            // pa and pb denote the
            // number of characters
            // a and b extracted
            int pa = 0, pb = 0;
     
            // If entire substring
            // of length k can be
            // extracted
            if (la - indexa >= k)
            {
                s = s + a.Substring(indexa, k);
                indexa = indexa + k;
                pa = k;
            }
     
            // If the remaining string
            // is of length less than k
            else if (la - indexa < k &&
                     la - indexa > 0)
            {
                s = s + a.Substring(indexa,
                                    la - indexa);
                pa = la - indexa;
                indexa = la;
            }
     
            // If the string has
            // been traversed
            else if (indexa >= la)
                pa = 0;
     
            // If entire substring
            // of length k can be
            // extracted
            if (lb - indexb >= k)
            {
                s = s + b.Substring(indexb, k);
                pb = k;
                indexb = indexb + k;
            }
     
            // If the remaining string
            // is of length less than k
            else if (lb - indexb < k &&
                     lb - indexb > 0)
            {
                s = s + b.Substring(indexb,
                               lb - indexb);
                pb = lb - indexb;
                indexb = lb;
            }
     
            // If the string has
            // been traversed
            else if (indexb >= lb)
                pb = 0;
            l = l - pa - pb;
        }
        Console.Write(s);
    }
     
    // Driver Code
    static void Main()
    {
        string a = "determination",
               b = "stance";
        int k = 3;
        solve(a, b, k);
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

Output : 

detstaermnceination

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :