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++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
// Function to encode the string
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;
}
// Driver code
int main()
{
string S = "GeeksForGeeks";
int R = 5;
cout << printRowWise(S, R);
return 0;
}
// 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 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# 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.
<script>
// JavaScript code for the above approach
// Function to encode the string
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('');
}
// Driver code
let S = "GeeksForGeeks";
let R = 5;
document.write(printRowWise(S, R));
// This code is contributed by Potta Lokesh
</script>
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.