Simple BruteForce Solution | ✅Beats 100% | C++| Java | Python | JavaScript
\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n## Connect with me on Linkedin Bijoy Sing.  \n\n###### Solution in C++, Python, Java, and JavaScript\n\ncpp [
\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n## Connect with me on Linkedin [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/).  \n\n###### *Solution in C++, Python, Java, and JavaScript*\n\n```cpp []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int row = box.size();\n        int col = box[0].size();\n        vector<vector<char>> box_90(col, vector<char>(row));\n\n        // Simulate gravity\n        for (int i = 0; i < row; i++) {\n            int cell = col - 1;\n            for (int j = col - 1; j >= 0; j--) {\n                if (box[i][j] == \'*\') {\n                    cell = j - 1;\n                } else if (box[i][j] == \'#\') {\n                    box[i][j] = \'.\';\n                    box[i][cell--] = \'#\';\n                }\n            }\n        }\n\n        // Rotate the box 90 degrees clockwise\n        for (int i = 0; i < row; i++) {\n            for (int j = 0; j < col; j++) {\n                box_90[j][row - i - 1] = box[i][j];\n            }\n        }\n\n        return box_90;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        row, col = len(box), len(box[0])\n        box_90 = [[""] * row for _ in range(col)]\n\n        # Simulate gravity\n        for i in range(row):\n            cell = col - 1\n            for j in range(col - 1, -1, -1):\n                if box[i][j] == \'*\':\n                    cell = j - 1\n                elif box[i][j] == \'#\':\n                    box[i][j] = \'.\'\n                    box[i][cell] = \'#\'\n                    cell -= 1\n\n        # Rotate the box 90 degrees clockwise\n        for i in range(row):\n            for j in range(col):\n                box_90[j][row - i - 1] = box[i][j]\n\n        return box_90\n```\n\n```java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int row = box.length, col = box[0].length;\n        char[][] box_90 = new char[col][row];\n\n        // Simulate gravity\n        for (int i = 0; i < row; i++) {\n            int cell = col - 1;\n            for (int j = col - 1; j >= 0; j--) {\n                if (box[i][j] == \'*\') {\n                    cell = j - 1;\n                } else if (box[i][j] == \'#\') {\n                    box[i][j] = \'.\';\n                    box[i][cell--] = \'#\';\n                }\n            }\n        }\n\n        // Rotate the box 90 degrees clockwise\n        for (int i = 0; i < row; i++) {\n            for (int j = 0; j < col; j++) {\n                box_90[j][row - i - 1] = box[i][j];\n            }\n        }\n\n        return box_90;\n    }\n}\n```\n\n```javascript []\nclass Solution {\n    rotateTheBox(box) {\n        const row = box.length, col = box[0].length;\n        const box_90 = Array.from({ length: col }, () => Array(row).fill(\'\'));\n\n        // Simulate gravity\n        for (let i = 0; i < row; i++) {\n            let cell = col - 1;\n            for (let j = col - 1; j >= 0; j--) {\n                if (box[i][j] === \'*\') {\n                    cell = j - 1;\n                } else if (box[i][j] === \'#\') {\n                    box[i][j] = \'.\';\n                    box[i][cell--] = \'#\';\n                }\n            }\n        }\n\n        // Rotate the box 90 degrees clockwise\n        for (let i = 0; i < row; i++) {\n            for (let j = 0; j < col; j++) {\n                box_90[j][row - i - 1] = box[i][j];\n            }\n        }\n\n        return box_90;\n    }\n}\n```\n\n---\n\n### Explanation:\n\n1. **Simulate Gravity**: Stones (`#`) in each row move down to the lowest available position, avoiding obstacles (`*`).\n2. **Rotate the Box**: The box is rotated 90 degrees clockwise, transforming rows into columns in the rotated box.\n3. **Return Result**: The new matrix (`box_90`) is returned, representing the rotated box after applying gravity.\n\n---\n\n### Approach:\n1. Apply gravity to the box to move stones (`#`) down.\n2. Rotate the box by mapping elements from rows to columns in a clockwise order.\n3. Combine the results into a new matrix and return it.\n\n---\n\n\n\n### Complexity:\n- **Time Complexity**:  \n  O(n * m), where n is the number of rows and m is the number of columns. Both gravity simulation and rotation involve iterating through the entire matrix.\n\n- **Space Complexity**:  \n  O(n * m), for the rotated box matrix (`box_90`).\n---\n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*\n\n--- \n
['Array', 'Two Pointers', 'Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']