Given a grid of size n x len filled with lowercase characters. We can swap two adjacent characters in the same row and column. Now we have to check whether it is possible to arrange in such a order that every row and every column in the grid is lexicographically sorted.
Input : abcde fghij olmkn trpqs xywuv Output : Yes Explanation : The grid can be rearranged as abcde fghij klmno pqrst uvwxy
The idea to do the above problem is really simple we can simply sort the characters in the same row and then just
check column vise if the new grid is sorted column vise or not. Please not that sorting is possible with adjacent swaps (Bubble sort for example does only adjacent swaps)
The implementation of the above idea is given below.
This article is contributed by Sarthak Kohli. 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.
- Generate permutations with only adjacent swaps allowed
- Number of swaps to sort when only adjacent swapping allowed
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Minimum adjacent swaps required to Sort Binary array
- Minimum number of adjacent swaps to convert a string into its given anagram
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Minimum product in a grid of adjacent elements
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Check if the robot is within the bounds of the grid after given moves
- Check if possible to shuffle a matrix with adjacent movements
- Check if it is possible to sort an array with conditional swapping of adjacent allowed
- Check if array can be sorted with one swap
- Check if a Linked List is Pairwise Sorted
- Python sorted() to check if two strings are anagram or not