Rotating the box - Easy Explanation with unique solution ✌️🔜
Intuition\n\n1. Initialize the whole output matrix whose size is col x row with \'.\' and iterate through each element of the matrix.\n\n1. We have also coming 
# Intuition\n\n1. Initialize the whole output matrix whose size is col x row with \'.\' and iterate through each element of the matrix.\n\n1. We have also coming from last element of the column in a matrix.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n**Initialization**:\n\nInitialize rows to be the number of rows in the box matrix and cols to be the number of columns at each row of the matrix.\n\n    int rows = box.length;\n    int cols = box[0].length;\n\nInitialize rotatedBox matrix with the size of cols x rows.\n\n    char[][] rotatedBox = new char[cols][rows];\n\n**Initialize rotated matrix**:\n\nInitialize all the elements of the rotatedBox to be \'.\' by iterating over all elements of the matrix from 0 to cols as outer loop iteration and 0 to rows as inner loop iteration.\n\n    for (int i = 0; i < cols; i++) {\n        for (int j = 0; j < rows; j++) {\n            rotatedBox[i][j] = \'.\';\n        }\n    }\n\n**Rotating the box computation**:\n\nIterate through each row of a matrix from 0 to rows.\n\n    for (int i = 0; i < rows; i++) {\n\nemptyIndex initially is set to the cols-1 since all the elements and calculate the actual empty index from bottom.\n\n    int emptyIndex = cols - 1;\n\nIterate through each elements in the row by iterating from cols-1 till the element reaches the index of 0.\n\n    for (int j = cols - 1; j >= 0; j--) {\n\n**Check some conditions**:\n\nIf the element from the rightmost is itself a stone, then assign the rotatedBox matrix with row index as that emptyIndex and the rows-i-1 to be the stone (\'#\').\n\nDecrement the emptyIndex by 1.\n\n    if (box[i][j] == \'#\') {\n        // Move stone to the farthest right position\n        rotatedBox[emptyIndex][rows - i - 1] = \'#\';\n        emptyIndex--;\n    }\n\nIf the element is a stationary obstacle(\'*\'), so assign the rotatedBox with row index as j and col index to be row-i-1 to be assigned that obstacle itself(\'*\').\n\nDecrement the emptyIndex count by j-1.\n\n    else if (box[i][j] == \'*\') {\n        // Place obstacle in the rotated matrix\n        rotatedBox[j][rows - i - 1] = \'*\';\n        emptyIndex = j - 1; // Reset emptyIndex after obstacle\n        }\n\n\n**Returning a value**:\n\nFinally rotatedBox matrix is returned which will have the matrix on rotation of the box in clockwise direction.\n\n    return rotatedBox;\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- ***Time complexity***: **O(colsxrows)**.\n      cols -> number of columns in the box.\n      rows -> number of rows in the box.\n\n**Initializing the rotatedBox**: **O(colsxrows)**.\n\nThis is the nested loop while runs for all elements of the box array and hence the number of elements in the box matrix is **colsxrows** and hence it\'s time complexity is **O(colsxrows)**.\n\n**Processing each row**: **O(colsxrows)**.\n\nThe loop runs for rows iteration.The inner loop processes each column in reverse, running for **cols iteration for each rows**.Hence the time complexity is **O(colsxrows)**.\n\n**Overall Time Complexity**: **O(colsxrows)**.\n\nThe time complexity for initializing the rotatedBox matrix and also for processing each row.Hence the overall time complexity is:\n\n**O(colsxrows) + O(colsxrows) = O(colsxrows)**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- ***Space complexity***: **O(colsxrows)**.\n    cols -> number of columns in the matrix.\n    rows -> number of rows in the matrix.\n\n**Rotated Matrix Storage**: **O(colsxrows)**.\n\nThe rotatedBox matrix requires colsxrows space and hence it\'s space complexity is **O(colsxrows)**.\n\n**Temporary variables**: **O(1)**.\n\nVariables like i, j, emptyIndex, and loop counters takes **O(1) space**.\n\n**Overall Space Complexity**: **O(colsxrows)**.\n\nSpace complexity is **dominated by** the storage for rotatedBox matrix and hence the space complexity is **O(colsxrows)**.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int rows = box.length;\n        int cols = box[0].length;\n\n        // Create the rotated matrix with swapped dimensions (cols x rows)\n        char[][] rotatedBox = new char[cols][rows];\n\n        // Initialize the rotated matrix with \'.\'\n        for (int i = 0; i < cols; i++) {\n            for (int j = 0; j < rows; j++) {\n                rotatedBox[i][j] = \'.\';\n            }\n        }\n\n        // Process each row and directly map elements to the rotated matrix\n        for (int i = 0; i < rows; i++) {\n            int emptyIndex = cols - 1; // Track the rightmost empty position for the current row\n            for (int j = cols - 1; j >= 0; j--) {\n                if (box[i][j] == \'#\') {\n                    // Move stone to the farthest right position\n                    rotatedBox[emptyIndex][rows - i - 1] = \'#\';\n                    emptyIndex--;\n                } else if (box[i][j] == \'*\') {\n                    // Place obstacle in the rotated matrix\n                    rotatedBox[j][rows - i - 1] = \'*\';\n                    emptyIndex = j - 1; // Reset emptyIndex after obstacle\n                }\n            }\n        }\n\n        return rotatedBox;\n    }\n}\n\n\n\n```
['Array', 'Matrix', 'Java']