Problem statement

https://leetcode.com/problems/rotating-the-box/

Solution

For each row we need to move stones from right to left. Start from the rightest one and simulate process.

Complexity

It is O(mn) for time and space.

Code

class Solution:
    def rotateTheBox(self, box):
        m, n = len(box), len(box[0])
        for row in box:
            pos = n - 1
            for j in range(n - 1, -1, -1):
                if row[j] == "*":
                    pos = j - 1
                elif row[j] == "#":
                    row[pos], row[j] = row[j], row[pos]
                    pos -= 1

        return zip(*box[::-1])