Detailed Explanation With Clean Code | Diagonal Traversal
\n\n>  You can solve this problem using the diagram given in sample testcase\n\n\n\n\n\n\nIf you are just confused about the diagonal traversal, you can see the
<br/>\n\n>  You can solve this problem using the diagram given in sample testcase\n\n<br/>\n<img src="https://assets.leetcode.com/uploads/2021/10/26/exam1.png" height="200px" />\n\n<br/>\n\nIf you are just confused about the diagonal traversal, you can see the last part of this blog :)\n\n**Steps:**\n\n* cols = encodedText.size() / rows\n* Fill all the characters in a 2D vector of size rows * cols (including spaces)\n* Add all the diagonals to answer string\n* Remove trailing zeros\n\nFor the example shown in above image when you traverse diagonally, you will get these substrings\n```\n"i lo" \n"ve l"\n"eetc"\n"ode"\n"  "\n" "\n```\nAnd when you keep adding these character by character, you will have original text as\n`"i love leetcode  "`\nthen you can remove the trailing spaces\n<br/>\n\n```\nclass Solution {\npublic:\n    string decodeCiphertext(string encoded, int rows) {\n        \n        int n = encoded.size();\n        int cols = n / rows;\n        vector<vector<char>> arr(rows, vector<char>(cols));\n        int k = 0;\n        \n\t\t// filling the vector\n        for(int i = 0; i < rows; i++) {\n            for(int j = 0; j < cols; j++) {\n                arr[i][j] = encoded[k++];\n            }\n        }\n        \n        string original = "";\n        \n\t\t// diagonal traversal as shown in image \n        for(int col = 0; col < cols; col++) {\n            int r = 0;\n            int c = col;\n            while(c < cols and r < rows) {\n                original += arr[r++][c++];\n            }\n        }\n        \n\t\t// removing trailing zeros\n        while(original.back() == \' \') original.pop_back();\n        \n        return original;\n    }\n};\n```\n\n<br/>\n\n**If you are confused about how to traverse diagonally:**\n\n<p>Traversing diagonals in a matrix simply means going from `row = 0; row < rows` and `col = 0; col < cols\n` by doing `row++, col++` at each stage\n<br/>\n\n**Diagonal traversal for matrix of size (4 * 6) (Shown in image) will be as follows:**\n\n```\n(0, 0) -> (1, 1) -> (2, 2) -> (3, 3)\n(0, 1) -> (1, 2) -> (2, 3) -> (3, 4)\n(0, 2) -> (1, 3) -> (2, 4) -> (3, 5)\n(0, 3) -> (1, 4) -> (2, 5)\n(0, 4) -> (1, 5)\n(0, 5)\n```\n\n</p>\n\n**Time complexity = Space complexity = O(n) = O(rows * cols)**\n\n<br/>
['C', 'C++']