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 firstname.lastname@example.org. 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
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum adjacent swaps required to Sort Binary array
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Sum of the count of number of adjacent squares in an M X N grid
- 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 whether two strings can be made equal by copying their characters with the adjacent ones
- Check if array can be sorted with one swap