Generate a string whose all K-size substrings can be concatenated to form the given string
Last Updated :
15 Nov, 2022
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:
- 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.
- Hence, traverse the string and append the first character of every substring to ans and then ignore next the K-1 characters.
- Repeat this process for all substrings except the last substring.
- Append all characters of the last substring to ans.
- Return ans as the required decoded string.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void decode_String(string str,
int K)
{
string ans = "" ;
for ( int i = 0; i < str.size();
i += K)
ans += str[i];
for ( int i = str.size() - (K - 1);
i < str.size(); i++)
ans += str[i];
cout << ans << endl;
}
int main()
{
int K = 3;
string str = "abcbcscsesesesd" ;
decode_String(str, K);
}
|
Java
class GFG{
public static void decode_String(String str,
int K)
{
String ans = "" ;
for ( int i = 0 ;
i < str.length(); i += K)
ans += str.charAt(i);
for ( int i = str.length() - (K - 1 );
i < str.length(); i++)
ans += str.charAt(i);
System.out.println(ans);
}
public static void main(String[] args)
{
int K = 3 ;
String str = "abcbcscsesesesd" ;
decode_String(str, K);
}
}
|
Python3
def decode_String(st, K):
ans = ""
for i in range ( 0 , len (st), K):
ans + = st[i]
for i in range ( len (st) - (K - 1 ), len (st)):
ans + = st[i]
print (ans)
if __name__ = = "__main__" :
K = 3
st = "abcbcscsesesesd"
decode_String(st, K)
|
C#
using System;
class GFG{
public static void decode_String(String str,
int K)
{
String ans = "" ;
for ( int i = 0;
i < str.Length; i += K)
ans += str[i];
for ( int i = str.Length - (K - 1);
i < str.Length; i++)
ans += str[i];
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int K = 3;
String str = "abcbcscsesesesd" ;
decode_String(str, K);
}
}
|
Javascript
<script>
function decode_String(str, K)
{
let ans = "" ;
for (let i = 0;
i < str.length; i += K)
ans += str[i];
for (let i = str.length - (K - 1);
i < str.length; i++)
ans += str[i];
document.write(ans);
}
let K = 3;
let str = "abcbcscsesesesd" ;
decode_String(str, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...