Java | 100% Faster | Time Complexity : O(m*n), Space : O(1)
Intuition\nAfter shifting value should be same, so we just need to check value at every index of matrix for \'before shifting case\' and \'after shifting case\'
# Intuition\nAfter shifting value should be same, so we just need to check value at every index of matrix for \'before shifting case\' and \'after shifting case\'. We don\'t need to check separately for odd and even rows.\n\n# Approach\nWe don\'t need to separately check for odd and even rows.\nJust check whether matrix is same before and after shifting k time.\n\nFor that we would use the following approach:\n\nValue of mat[i][j] = Value of mat[i][j + k]\nThis means value of mat[i][j] should be equal to value of mat after shifting elements k times.\n\nExample : \nLet\'s take example for 1D matrix : mat[3] = [1,2,3] and k = 2.\nNow after right shift k times, we get mat[3] = [2,3,1].\n\nSo we just need to compare values at ith index for every element for \'before case\' and \'after case\'.\n\n# Complexity\n- Time complexity:O(m*n)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution {\n    public boolean areSimilar(int[][] mat, int k) {\n        int m = mat.length;\n        int n = mat[0].length;\n\n        for(int i = 0; i < m; i++) {\n            for(int j = 0; j < n; j++) {\n                if(mat[i][j] != mat[i][(j+k) % n])\n                    return false;\n            }\n        }\n        return true;\n    }\n}\n```
['Java']