GFG App
Open App
Browser
Continue

# 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
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

 `// C++ code to implement above approach` `#include ` `using` `namespace` `std;`   `// Function to encode the string` `string printRowWise(string S, ``int` `R)` `{` `    ``vector 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;` `}`   `// Driver code` `int` `main()` `{` `    ``string S = ``"GeeksForGeeks"``;` `    ``int` `R = 5;` `    ``cout << printRowWise(S, R);` `    ``return` `0;` `}`

## Java

 `// Java code to implement above approach` `import` `java.util.*;` `class` `GFG{`   `  ``// Function to encode the String` `  ``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;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``String S = ``"GeeksForGeeks"``;` `    ``int` `R = ``5``;` `    ``System.out.print(printRowWise(S.toCharArray(), R));` `  ``}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# python3 code to implement above approach`   `# Function to encode the string` `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`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``S ``=` `"GeeksForGeeks"` `    ``R ``=` `5` `    ``print``(printRowWise(S, R))`   `# This code is contributed by rakeshsahni`

## C#

 `// C# code to implement above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG {`   `  ``// Function to encode the string` `  ``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;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{` `    ``string` `S = ``"GeeksForGeeks"``;` `    ``int` `R = 5;` `    ``Console.Write(printRowWise(S, R));` `  ``}` `}`   `// This code is contributed by ukasp.`

## Javascript

 ``

Output

`GFeeokerskGse`

Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(R), where R is the no of rows.

My Personal Notes arrow_drop_up