Encode given String by inserting in Matrix column-wise and printing it row-wise
Given a string S and an integer R, the task is to encode the string by first filling each character in column wise manner from top to bottom in a matrix having R rows and then printing the matrix row-wise.
Examples:
Input: S = “abcdefgh”, R = 3
Output: adgbehcf
Explanation: Matrix formed is:
a d g
b e h
c f
So when printed row wise it gives the encoded string as “adgbehcf”
Input: S = “GeeksForGeeks”, R = 5
Output: GFeeokerskGse
Explanation: Pattern formed is:
G f e
e o k
e r s
k G
s e
Approach: The approach is to use an array of string temp of size R. Traverse every character of string S and add it at the end of the string at i%R index of the array temp. Now traverse the temp array row-wise to get the encoded string.
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
string printRowWise(string S, int R)
{
vector<string> temp(R);
string ans;
for ( int i = 0; i < S.length(); i++)
temp[i % R].push_back(S[i]);
for ( int i = 0; i < R; i++) {
for ( int j = 0; j < temp[i].size();
j++)
ans.push_back(temp[i][j]);
}
return ans;
}
int main()
{
string S = "GeeksForGeeks" ;
int R = 5;
cout << printRowWise(S, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String printRowWise( char []S, int R)
{
String []temp = new String[R];
String ans= "" ;
for ( int i = 0 ; i < S.length; i++) {
if (temp[i % R] == null )
temp[i % R] = "" ;
temp[i % R] += (S[i]);
}
for ( int i = 0 ; i < R; i++) {
for ( int j = 0 ; j < temp[i].length();
j++)
ans+=(temp[i].charAt(j));
}
return ans;
}
public static void main(String[] args)
{
String S = "GeeksForGeeks" ;
int R = 5 ;
System.out.print(printRowWise(S.toCharArray(), R));
}
}
|
Python3
def printRowWise(S, R):
temp = ["" for _ in range (R)]
ans = ""
for i in range ( 0 , len (S)):
temp[i % R] + = S[i]
for i in range ( 0 , R):
for j in range ( 0 , len (temp[i])):
ans + = temp[i][j]
return ans
if __name__ = = "__main__" :
S = "GeeksForGeeks"
R = 5
print (printRowWise(S, R))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string printRowWise( string S, int R)
{
string [] temp = new string [R];
string ans = "" ;
for ( int i = 0; i < S.Length; i++)
temp[i % R] += S[i];
for ( int i = 0; i < R; i++) {
for ( int j = 0; j < temp[i].Length; j++)
ans += (temp[i][j]);
}
return ans;
}
public static void Main()
{
string S = "GeeksForGeeks" ;
int R = 5;
Console.Write(printRowWise(S, R));
}
}
|
Javascript
<script>
function printRowWise(S, R) {
let temp = new Array(R);
for (let i = 0; i < R; i++) {
temp[i] = []
}
let ans = [];
for (let i = 0; i < S.length; i++)
temp[i % R].push(S[i]);
for (let i = 0; i < R; i++) {
for (let j = 0; j < temp[i].length;
j++)
ans.push(temp[i][j]);
}
return ans.join( '' );
}
let S = "GeeksForGeeks" ;
let R = 5;
document.write(printRowWise(S, R));
</script>
|
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(R), where R is the no of rows.
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...