Merge two strings in chunks of given size
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:
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 calcuations 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 = 3solve(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
Recommended Posts:
- A Program to check if strings are rotations of each other or not
- Check whether two strings are anagram of each other
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Find if a string is interleaved of two other strings | DP-33
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
- Print all possible strings that can be made by placing spaces
- Find all distinct palindromic sub-strings of a given string
- Check if two given strings are isomorphic to each other
- Check if edit distance between two strings is one
- Find all strings formed from characters mapped to digits of a number
- All combinations of strings that can be used to dial a number
- Generate all binary strings from given pattern
- Concurrent Merge Sort in Shared Memory
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1



