Intuitive python 0ms O(n) solution - Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nWe could solve this problem by following the greedy approach. A more effecient way coul
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe could solve this problem by following the greedy approach. A more effecient way could be to find out the total number of transformations which we need to apply to the matrix and apply it directly using only one loop.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe first copy **mat** and store it in **x**\nThen we calculate the number of columns in **mat** and store it in **l**.\nthen we start the loop. \nfirst we calculate how many transformations we **ACTUALLY** need to perform. Because when **k** == **l** we actually get the original order back. \nFor example, \nif l = 3 and k = 4 then we actually need to transform 1 time as on the third transformation the value order will be same as original and the fourth transformation will be the same as the first transformation. \nWe need to keep substracting **k** with **l** until we get a value which is less than **k**\nin case of **k** = 7 and **l** = 2, we\'ll get 5 after the first substaction which is still greater than **l**\n\nLastly, transform the list!\n**For even rows:** \n1. Make a sub-list from 0 to kth value.\n2. Remove 0 to kth value from the main list by making a sub-list from kth value till the last one and assigning it to the main list\n3. Append the first sub-list to main list\n\n**For odd rows**\n1. Make a sub-list from kth value till the last one\n2. Remove kth value till the last one from the main list by creating a sub-list from 0 to kth value and assigning it to the main list.\n3. add the sub-array to the first of the main array\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def areSimilar(self, mat: List[List[int]], k: int) -> bool:\n        x = mat.copy()\n        l = len(mat[0])\n      \n        for i in range(len(mat)):\n            while k > l:\n                k = k - l\n            if i % 2 == 0:\n                t = x[i][:k]\n                x[i] = x[i][k:] \n                x[i] += t \n            else:\n                t = x[i][k:]\n                x[i] = x[i][:k]\n                x[i] = t + x[i]\n \n        return x == mat\n       \n\n```
['Python3']