Merge two strings in chunks of given size
Last Updated :
17 Nov, 2022
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.
Implementation:
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
// https://www.geeksforgeeks.org/stdsubstr-in-ccpp/
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)
JavaScript
// Javascript program to merge n number of strings
// Function performing the calculations
function solve(a, b, k)
{
let s = ""
// Length of string a
let la = a.length
// Length f string b
let lb = b.length
let l = la + lb
// Pointers for string
// a and string b
let indexa = 0
let indexb = 0
while (l > 0) {
// pa and pb denote the number
// of characters of both
// a and b extracted
let pa = 0
let pb = 0;
// If entire substring of length
// k can be extracted
if (la - indexa >= k) {
// Please refer below link for
// details of library function
// https://www.geeksforgeeks.org/stdsubstr-in-ccpp/
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
}
console.log(s)
}
// Driver function
let a = "determination"
let b = "stance"
let k = 3
solve(a, b, k)
// This code is contributed by Samim Hossain Mondal.
Outputdetstaermnceination
Time Complexity: O(|a|+|b|).
Auxiliary Space: O(|a|+|b|), where |a| and |b| are the length of string a and b respectively.
Similar Reads
Print all interleavings of given two strings Given two strings str1 and str2, write a function that prints all interleavings of the given two strings. You may assume that all characters in both strings are different Example: Input: str1 = "AB", str2 = "CD"Output: ABCD ACBD ACDB CABD CADB CDABInput: str1 = "AB", str2 = "C"Output: ABC ACB CABAn
14 min read
Count of lines required to write the given String Given a string str and an integer array width[] where: width[0] = width of character 'a' width[1] = width of character 'b' ... width[25] = width of character 'z' The task is to find the number of lines it'll take to write the string str on a paper and the width of the last line upto which it is occu
6 min read
Check if a String is Interleaving of Other Two Give three strings s1, s2 and s3, determine if s3 is formed by interleaving s1 and s2.A string s3 is an interleaving of s1 and s2 if:It contains all characters of s1 and s2 while preserving their relative order.Characters from s1 and s2 appear in s3 in the same order as in their original strings.The
15+ min read
Alternatively Merge two Strings Given 2 strings, merge them in an alternate way, i.e. the final string's first character is the first character of the first string, the second character of the final string is the first character of the second string and so on. And if once you reach end of one string while if another string is stil
4 min read
Find the longest Substring of a given String S Given a string S of length, N. Find the maximum length of any substring of S such that, the bitwise OR of all the characters of the substring is equal to the bitwise OR of the remaining characters of the string. If no such substring exists, print -1. Examples: Input: S = "2347"Output: 3?Explanation:
10 min read
Count number of distinct substrings of a given length Given a string S of length N consisting of lower-case English alphabets and an integer 'l', find the number of distinct substrings of length 'l' of the given string. Examples: Input : s = "abcbab", l = 2 Output : 4 All distinct sub-strings of length 2 will be {"ab", "bc", "cb", "ba"} Thus, answer eq
6 min read
Length of longest prefix anagram which are common in given two strings Given two strings str1 and str2 of the lengths of N and M respectively, the task is to find the length of the longest anagram string that is prefix substring of both strings. Examples: Input: str1 = "abaabcdezzwer", str2 = "caaabbttyh"Output: 6Explanation: Prefixes of length 1 of string str1 and str
8 min read
Minimizing Steps to Form Anagrams from Given Strings Given two strings s1 and s2. You have the flexibility to add any letter to either the string s1 or s2 in just one action. Find out the least number of steps needed to transform two given words, s1, and s2, into anagrams of each other. The length of both strings can be different and it contains only
7 min read
Minimize partitions in given string to get another string Given two strings A and B, print the minimum number of slices required in A to get another string B. In case, if it is not possible to get B from A, then print "-1". Examples : Input: A = "geeksforgeeks", B = "ksgek"Output: 5Explanation: g | ee | ks | forge | ek | s : minimum 5 slices are required t
15+ min read
Enclose given Substrings of the String in parenthesis Given a string S and a list of strings subs[] that stores the substrings of S and all the substrings are present only once, the task is to enclose the substrings of S that exists in subs[] in parentheses. If substrings in subs[] overlap each other or are consecutive then merge them into one set of p
11 min read