Generate a string whose all K-size substrings can be concatenated to form the given string

Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string.

Examples:

Input: str = “abbaaa” K = 2
Output: abaa
Explanation:
All substring of size 2 of parent string “abaa” are “ab”, “ba” and “aa”. After concatenating all these substrings, the given string “abbaaa” can be obtained.

Input: str = “abcbcscsesesesd” K = 3
Output: abcsesd
Explanation :
All substring of size 3 of parent string “abcsesd” are “abc”, “bcs”, “cse”, “ses” and “esd”. After concatenating all these substrings, the given string “abcbcscsesesesd” can be obtained.

Approach:
Follow the steps below to solve the problem:



  1. We can clearly observe that by concatenating substrings of length K, except the first character, the remaining K-1 characters of any substring is present in the next substring as well.
  2. Hence, traverse the string and append the first character of every substring to ans and then ignore next the K-1 characters.
  3. Repeat this process for all substrings except the last substring.
  4. Append all characters of the last substring to ans.
  5. Return ans as the required decoded string.

Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to generate a 
// string whose substrings of
// length K concatenates to 
// form given strings
  
#include <bits/stdc++.h> 
using namespace std;
  
// Function to return the required 
// required string 
void decode_String(string str, 
int K)
{
    string ans = "";
    // Iterate the given string
    for (int i = 0; i < str.size(); 
    i += K)
        // Append the first 
        // character of every 
        // substring of length K
        ans += str[i];
  
    // Consider all characters 
    // from the last substring
    for(int i = str.size() - (K - 1); 
    i < str.size(); i++)
        ans += str[i];
  
    cout << ans << endl;
}
  
// Driver Program
int main()
{
    int K = 3;
    string str = "abcbcscsesesesd";
    decode_String(str, K);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to generate a 
// string whose substrings of 
// length K concatenates to 
// form given strings 
class GFG{
      
// Function to return the required 
// required string 
public static void decode_String(String str,
                                 int K) 
    String ans = ""
      
    // Iterate the given string 
    for(int i = 0
            i < str.length(); i += K) 
         
       // Append the first 
       // character of every 
       // substring of length K 
       ans += str.charAt(i); 
      
    // Consider all characters 
    // from the last substring 
    for(int i = str.length() - (K - 1);
            i < str.length(); i++) 
       ans += str.charAt(i); 
      
    System.out.println(ans);
  
// Driver code
public static void main(String[] args)
{
    int K = 3
    String str = "abcbcscsesesesd";  
      
    decode_String(str, K); 
}
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to generate a string
// whose substrings of length K
// concatenates to form given strings 
using System;
  
class GFG{
      
// Function to return the required 
// required string 
public static void decode_String(String str,
                                 int K) 
    String ans = ""
      
    // Iterate the given string 
    for(int i = 0; 
            i < str.Length; i += K) 
              
        // Append the first 
        // character of every 
        // substring of length K
        ans += str[i]; 
      
    // Consider all characters 
    // from the last substring 
    for(int i = str.Length - (K - 1);
            i < str.Length; i++) 
        ans += str[i]; 
      
    Console.WriteLine(ans);
  
// Driver code
public static void Main(String[] args)
{
    int K = 3; 
    String str = "abcbcscsesesesd"
      
    decode_String(str, K); 
}
}
  
// This code is contributed by Rohit_ranjan

chevron_right


Output:

abcsesd

Time Complexity: O(N)
Auxiliary Space: O(1)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.