Simple 2D Matrix creation ✅✅ | C++ Easy
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition is to construct a matrix from the encoded text and then iterate through t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to construct a matrix from the encoded text and then iterate through the matrix to form the decoded text.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1.)** **Matrix Construction:** Calculate the number of columns based on the length of the encoded text and the given number of rows. Create a matrix to represent the encoded text, filling it with characters from the encoded text in a column-wise manner.\n\n**2.)** **Traverse Matrix:** Traverse the matrix in a slanted manner (from top-left to bottom-right), extracting characters and forming the decoded text. Adjust the indexing to follow the desired traversal pattern.\n\n**3.)** **Trim Trailing Spaces:** Remove any trailing spaces from the decoded text.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the length of the encoded text. \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n*n)$$, as additional space is used to store the matrix.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string decodeCiphertext(string encodedText, int rows) {\n    int sz = encodedText.size();\n        int cols = sz / rows;\n\n        vector<vector<char>> matrix(rows, vector<char>(cols, \' \'));\n        int index = 0;\n        for (int col = 0; col < cols; ++col) {\n            for (int row = 0; row < rows; ++row) {\n                matrix[row][col] = encodedText[index++];\n            }\n        }\n        string decodedText;\n        for (int i = 0; i < cols; ++i) {\n            for (int j = i; j < sz; j += cols + 1) {\n                decodedText += matrix[j % rows][j / rows];\n            }\n        }\n        size_t lastNonSpace = decodedText.find_last_not_of(\' \');\n        if (lastNonSpace != string::npos) {\n            decodedText = decodedText.substr(0, lastNonSpace + 1);\n        }\n\n        return decodedText;\n    }\n};\n```
['C++']