You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:


	A stone '#'
	A stationary obstacle '*'
	Empty '.'


The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.

It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.

Return an n x m matrix representing the box after the rotation described above.

&nbsp;
Example 1:



Input: box = [["#",".","#"]]
Output: [["."],
&nbsp;        ["#"],
&nbsp;        ["#"]]


Example 2:



Input: box = [["#",".","*","."],
&nbsp;             ["#","#","*","."]]
Output: [["#","."],
&nbsp;        ["#","#"],
&nbsp;        ["*","*"],
&nbsp;        [".","."]]


Example 3:



Input: box = [["#","#","*",".","*","."],
&nbsp;             ["#","#","#","*",".","."],
&nbsp;             ["#","#","#",".","#","."]]
Output: [[".","#","#"],
&nbsp;        [".","#","#"],
&nbsp;        ["#","#","*"],
&nbsp;        ["#","*","."],
&nbsp;        ["#",".","*"],
&nbsp;        ["#",".","."]]


&nbsp;
Constraints:


	m == box.length
	n == box[i].length
	1 &lt;= m, n &lt;= 500
	box[i][j] is either '#', '*', or '.'.

class Solution:
    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
        # move stones to right, row by row
        for i in range(len(box)):
            stone = 0
            for j in range(len(box[0])):
                if box[i][j] == '#': # if a stone
                    stone += 1
                    box[i][j] = '.'
                elif box[i][j] == '*': # if a obstacle
                    for m in range(stone):
                        box[i][j-m-1] = '#'
                    stone = 0
            # if reaches the end of j, but still have stone
            if stone != 0:
                for m in range(stone):
                        box[i][j-m] = '#'
        
        # rotate box, same as leetcode #48
        box[:]  = zip(*box[::-1])
        
        return box
 class Solution {
     public char[][] rotateTheBox(char[][] box) {
         int row = box.length, col = box[0].length;
         char[][] res = new char[col][row];
         // rotate first, then drop
         for (int i = 0; i < row; i++) {
             for (int j = 0; j < col; j++) {
                 res[j][i] = box[row-1-i][j];
             }
         }

        for (int i = col - 1; i >= 0; i--) {
             for (int j = 0; j < row; j++) {
                 if (res[i][j] == '#') {
                     int curRow = i;
                     while (curRow+1 < col && res[curRow+1][j] == '.') {
                         curRow++;
                     }
                     if (curRow != i) {
                         res[curRow][j] = '#';
                         res[i][j] = '.';
                     }
                 }
             }
         }
         return res;
     }
 }
class Solution {
public:
    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
        int m = box.size();
        int n = box[0].size();

        vector<vector<char>> ans(n,vector<char>(m,'.'));

        for(int i=0;i<m;++i){
            int k = n; //Stores last obstacle or occupied position
            for(int j=n-1;j>=0;--j)
            {
                if(box[i][j] == '#')
                {
                    ans[--k][i] = '#';
                }
                else if(box[i][j] == '*')
                {
                     k = j;
                     ans[j][i] = '*';
                }
            }
        }
        for(int i=0;i<n;++i){
            reverse(ans[i].begin(), ans[i].end());
        }
        return ans;
    }
};
var rotateTheBox = function(box) {
  for(let r=0; r<box.length;r++){
    let idx = null
    for(let c = 0;c<box[r].length;c++){
      const curr = box[r][c]
      if(curr === '*'){
        idx = null
        continue
      }
      if(curr === '#' && idx === null){
        idx = c
        continue
      }
      if(curr === '.' &&
         box[r][idx] === '#' &&
         idx <= c){
        box[r][idx] = '.'
        box[r][c] = '#'
        idx++
      }
    }
  }
  return transpose(box)
};

function transpose(arr){
  const B = []
  for(let c = 0; c < arr[0].length; c++){
    if(!B[c]) B[c] = []
    for(let r = 0; r < arr.length; r++){
      B[c][r] = arr[r][c]
    }
    B[c].reverse()
  }
  return B
}
Rotating the Box