In TypeScript, Beats 100% ✨
Here\u2019s the completed template based on your solution:\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires ch
Here\u2019s the completed template based on your solution:\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires checking if each row in a matrix can be rotated by a given offset `k` so that the resulting rows are similar to the original. My first thought was to consider how elements shift in even and odd-indexed rows when rotated by a given offset, using modular arithmetic to handle the wrapping around the matrix dimensions.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Calculate the remainder `rem` when `k` is divided by the number of columns `m`. This remainder represents the number of steps each element will shift in a single rotation within each row.\n2. For each row:\n   - If it\u2019s an even-indexed row, shift elements to the left by `rem`.\n   - If it\u2019s an odd-indexed row, shift elements to the right by `rem`.\n3. Check if each element in the shifted position matches the corresponding original element in each row.\n4. If all rows satisfy the condition, return `true`; otherwise, return `false`.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n  - $$O(n \\times m)$$, where `n` is the number of rows and `m` is the number of columns, as we iterate through each element in the matrix once.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n  - $$O(1)$$, as we only use a constant amount of extra space for variables and calculations.\n\n# Code\n```typescript\nfunction areSimilar(mat: number[][], k: number): boolean {\n    const n = mat.length;\n    const m = mat[0].length;\n    const rem = k % m;\n    // complete cycle\n    if ( rem === 0 ) return true;\n    \n    for (let i = 0; i < n; i++) {\n        if ( i % 2 === 0 ) {\n            for (let j = 0; j < m; j++) {\n                const idx = j - rem < 0 ? m + (j - rem) : j - rem;\n                if (mat[i][j] !== mat[i][idx]) return false;\n            }\n        } else {\n            for (let j = 0; j < m; j++) {\n                const idx = j + rem >= m ? j + rem - m : j + rem;\n                if (mat[i][j] !== mat[i][idx]) return false;\n            }\n        }\n    }\n\n    return true;\n}\n```
['Array', 'Math', 'Matrix', 'Simulation', 'TypeScript']