[C++] | Concise + Commented Code | Idea Explained
\n\nIdea\n Since the encoded text contains all the characters of the matrix, we can determine the number of columns of the matrix and then form the matrix.\n On
\n\n**Idea**\n* Since the encoded text contains all the characters of the matrix, we can determine the number of columns of the matrix and then form the matrix.\n* Once the matrix is ready, we need to traverse it diagonally to obtain the original text. After taking some examples it becomes clear that when a boundary is hit, the pointer for row resets to 0, and the pointer for column is subtracted by the *currentrow minus 1*. \n\n\n**C++**\n```\nclass Solution {\npublic:\n    string decodeCiphertext(string encodedText, int rows) {\n        int n = encodedText.size();\n        \n        // Determining the number of columns\n        int cols = n / rows;\n        vector<vector<char>> mat(rows, vector<char>(cols, \' \'));\n        int i = 0, j = 0;\n        int k = 0;\n        \n        \n        string ans = "";\n        \n        // Filling the matrix using encodedText\n        // Row wise\n        for(int i = 0; i < rows; i++) {\n            for(int j = 0; j < cols; j++) {\n                mat[i][j] = encodedText[k++];\n            }\n        }\n        \n        // Only the upper triangular part of the matrix will\n        // contain characters of the originalText\n        // so, this loop traverses that area\n        for(int k = 0; k < n - (rows * (rows - 1)) / 2; k++) {\n            // i, j are the two pointers for tracking rows and columns\n            ans.push_back(mat[i++][j++]);\n            \n            // If any boundary is hit, then column pointer is subtracted \n            // by row_pointer - 1\n            // and row pointer is reset to 0\n            if(i == rows || j == cols) {\n                j -= (i - 1);\n                i = 0;\n            }\n        }\n        \n        // Removing all trailing spaces\n        while(ans.back() == \' \')\n            ans.pop_back();\n        \n        return ans;\n    }\n};\n```\n\n**Complexity Analysis**\n*Space Complexity: O(n), where n is the size of encodedText*\n*Time Complexity: O(n), where n is the size of encodedText*
['C']