Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not.
Input: mat = 1, 2, 3 3, 1, 2 2, 3, 1 Output: Yes All rows are rotated permutation of each other. Input: mat = 1, 2, 3 3, 2, 1 1, 3, 2 Output: No Explanation : As 3, 2, 1 is not a rotated or circular permutation of 1, 2, 3
The idea is based on below article.
A Program to check if strings are rotations of each other or not
- Create a string of first row elements and concatenate the string with itself so that string search operations can be efficiently performed. Let this string be str_cat.
- Traverse all remaining rows. For every row being traversed, create a string str_curr of current row elements. If str_curr is not a substring of str_cat, return false.
- Return true.
Below is the implementation of above steps.
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- A Program to check if strings are rotations of each other or not
- Check if two numbers are bit rotations of each other or not
- Check if strings are rotations of each other or not | Set 2
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Check if the rows of a binary matrix can be made unique by removing a single column
- Find a common element in all rows of a given row-wise sorted matrix
- Common elements in all rows of a given matrix
- Count all sorted rows in a matrix
- Find all permuted rows of a given row in a matrix
- Find distinct elements common to all rows of a matrix
- Check whether two strings are anagram of each other
- Check if two given strings are isomorphic to each other
- Check if two strings are permutation of each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings can be made equal by swapping one character among each other
- C Program to check if two given strings are isomorphic to each other