Step by step Beginner's friendly explanation
Complexity\n- Time complexity: O(n^2) \n\n- Space complexity: O(n^2) \n\n# Code\n\nclass Solution {\npublic:\n    string decodeCiphertext(string encodedText, in
# Complexity\n- Time complexity: $$O(n^2)$$ \n\n- Space complexity: $$O(n^2)$$ \n\n# Code\n```\nclass Solution {\npublic:\n    string decodeCiphertext(string encodedText, int rows) {\n\n     //step 1 : find no. of columns needed in the matrix\n     int cols = encodedText.length()/rows;\n     string ans = ""; \n     int k = 0, i = 0, j = 0, nxtCol = 1;\n\n     //step 2 : form the matrix from the given string\n     vector<vector<char>> grid(rows,vector<char>(cols,\' \'));\n      for(int i=0; i<rows; i++){\n           for(int j=0; j<cols; j++){\n                 grid[i][j] = encodedText[k++];\n           }\n       }\n\n      //step 3 : form the decoded string by traversing the matrix diagonally\n       k = 0;\n       while(nxtCol <= cols)\n       {\n           if(i >= rows || j >= cols) //always keep i and j as valid index\n           {\n               i = 0;\n               j = nxtCol;\n               nxtCol++;\n           }\n          if(j < cols) ans += grid[i][j];\n          i++;\n          j++;\n       }\n\n      //step 4 : remove leading spaces, if any\n       while(!ans.empty() && ans.back() == \' \')\n        ans.pop_back();\n\n       return ans;\n    }\n};\n```
['String', 'Matrix', 'Simulation', 'C++']