Encode given String by inserting in Matrix column-wise and printing it row-wise Last Updated : 02 Aug, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 3Output: adgbehcfExplanation: Matrix formed is:a d gb e hc fSo when printed row wise it gives the encoded string as "adgbehcf" Input: S = "GeeksForGeeks", R = 5Output: GFeeokerskGseExplanation: Pattern formed is:G f ee o ke r sk Gs 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 <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 // 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 <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> OutputGFeeokerskGse Time Complexity: O(N), where N is the length of the stringAuxiliary Space: O(R), where R is the no of rows. Comment More infoAdvertise with us Next Article Encode given String by inserting in Matrix column-wise and printing it row-wise prophet1999 Follow Improve Article Tags : Strings Algo Geek DSA Algo-Geek 2021 encoding-decoding +1 More Practice Tags : Strings Similar Reads Find the original matrix when largest element in a row and a column are given Given two arrays A[] and B[] of N and M integers respectively. Also given is a N X M binary matrix where 1 indicates that there was a positive integer in the original matrix and 0 indicates that the position is filled with 0 in the original matrix. The task is to form back the original matrix such t 6 min read Print all elements in sorted order from row and column wise sorted matrix Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of the matrix in sorted order. Example: Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}, };Output: 10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50 Recomme 15+ min read Print index of columns sorted by count of zeroes in the Given Matrix Given a matrix with N rows and M columns. The task is to print the index of columns of the given matrix based on the increasing number of zeroes in each column. For Example, If the 1st column contains 2 zero's, the 2nd column contains 1 zero, and 3rd column does not contain any zeroes. Then the outp 8 min read Print an N x M matrix such that each row and column has all the vowels in it Given two integers N and M, the task is to print an N x M matrix such that each row and column contain all the vowels in it. If it is impossible to do so, then print -1. Examples: Input: N = 5, M = 5 Output: a e i o u e i o u a i o u a e o u a e i u a e i o Input: N = 6, M = 2 Output: -1 Approach: S 6 min read Modify a matrix by rotating ith row exactly i times in clockwise direction Given a matrix mat[][] of dimensions M * N, the task is to print the matrix obtained after rotating every ith row of the matrix i times in a clockwise direction. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output:1 2 36 4 58 9 7Explanation:The 0th row is rotated 0 times. Therefore, t 6 min read Shuffle the given Matrix K times by reversing row and columns alternatively in sequence Given a matrix arr[][] of size M * N, where M is the number of rows and N is the number of columns, and an integer K. The task is to shuffle this matrix in K steps such that in each step, the rows and columns of the matrix are reversed alternatively i.e start with reversing the 1st row first step, t 7 min read Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 3 min read Print exchange order and swap count to make count of 1s same rowwise in Binary Matrix Given a n*m binary matrix, the task is to make an equal number of 1âs in each row using the operation as follows: Choose any two rows from (1 to n) and a column from (1 to m) and swap the elements present at these two positions. If it is possible to make equal ones in each row then return the number 10 min read Decrypt the encoded string with help of Matrix as per given encryption decryption technique Given an encoded (or encrypted) string S of length N, an integer M. The task is to decrypt the encrypted string and print it. The encryption and decryption techniques are given as: Encryption: The original string is placed in a Matrix of M rows and N/M columns, such that the first character of the O 6 min read Program to reverse columns in given 2D Array (Matrix) Given a 2D array arr[][]of integers of size M x N, where N is the number of columns and M is the number of rows in the array. The task is to reverse every column of the given 2D array Input: arr[][] = {{3, 2, 1} {4, 5, 6}, {9, 8, 7}} Output: 9 8 7 4 5 6 3 2 1 Input: arr[][] = {{7, 9}, {1, 5}, {4, 6} 7 min read Like