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.

Examples:

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.

## C++

`// C++ program to check if we can make a ` `// grid of character sorted using adjacent ` `// swaps. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// v[] is vector of strings. len is length ` `// of strings in every row. ` `bool` `check(vector<string> v, ` `int` `len) ` `{ ` ` ` `int` `n = v.size(); ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sort(v[i].begin(), v[i].end()); ` ` ` ` ` `for` `(` `int` `i = 0; i < len-1; i++) ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `if` `(v[i][j] > v[i+1][j]) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<string> v = { ` `"ebcda"` `, ` `"ihgfj"` `, ` `"klmno"` `, ` ` ` `"pqrst"` `, ` `"yvwxu"` `}; ` ` ` `int` `len = 5; ` `// Length of strings ` ` ` `check(v, len)? cout << ` `"Yes"` `: cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if we can make a grid ` `// of character sorted using adjacent swaps. ` `import` `java.util.Arrays; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// v[] is vector of strings. len is ` ` ` `// length of strings in every row. ` ` ` `static` `boolean` `check(String[] v, ` `int` `len) ` ` ` `{ ` ` ` `int` `n = v.length; ` ` ` `char` `[] tempArray; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `tempArray = v[i].toCharArray(); ` ` ` `Arrays.sort(tempArray); ` ` ` `v[i] = ` `new` `String(tempArray); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < len-` `1` `; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` `if` `(v[i].charAt(j) > v[i+` `1` `].charAt(j)) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` ` ` `String[] v = { ` `"ebcda"` `, ` `"ihgfj"` `, ` `"klmno"` `, ` ` ` `"pqrst"` `, ` `"yvwxu"` `}; ` ` ` ` ` `int` `len = ` `5` `; ` `// Length of strings ` ` ` `if` `(check(v, len)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Python

`# Python program to check if we can make a ` `# grid of character sorted using adjacent ` `# swaps. ` ` ` `# v[] is vector of strings. len is length ` `# of strings in every row. ` `def` `check(v, l): ` ` ` `n ` `=` `len` `(v) ` ` ` `for` `i ` `in` `v: ` ` ` `i ` `=` `''.join(` `sorted` `(i)) ` ` ` ` ` `for` `i ` `in` `range` `(l ` `-` `1` `): ` ` ` `for` `j ` `in` `range` `(n): ` ` ` `if` `(v[i][j] > v[i ` `+` `1` `][j]): ` ` ` `return` `False` ` ` `return` `True` ` ` `# Driver code ` `v ` `=` `[ ` `"ebcda"` `, ` `"ihgfj"` `, ` `"klmno"` `, ` `"pqrst"` `, ` `"yvwxu"` `] ` `l ` `=` `5` `# Length of strings ` `if` `check(v, l): ` ` ` `print` `"Yes"` `else` `: ` ` ` `print` `"No"` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if we can make a grid ` `// of character sorted using adjacent swaps. ` `using` `System; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// v[] is vector of strings. len is ` ` ` `// length of strings in every row. ` ` ` `static` `Boolean check(String[] v, ` `int` `len) ` ` ` `{ ` ` ` `int` `n = v.Length; ` ` ` `char` `[] tempArray; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `tempArray = v[i].ToCharArray(); ` ` ` `Array.Sort(tempArray); ` ` ` `v[i] = ` `new` `String(tempArray); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 0; i < len-1; i++) ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `if` `(v[i][j] > v[i+1][j]) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` ` ` `String[] v = { ` `"ebcda"` `, ` `"ihgfj"` `, ` `"klmno"` `, ` ` ` `"pqrst"` `, ` `"yvwxu"` `}; ` ` ` ` ` `int` `len = 5; ` `// Length of strings ` ` ` `if` `(check(v, len)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Yes

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 contribute@geeksforgeeks.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.

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.

## Recommended Posts:

- Sort Matrix in alternating ascending and descending order rowwise
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Find if array can be sorted by swaps limited to multiples of k
- Largest number not greater than N which can become prime after rearranging its digits
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Check if a string can become empty by recursively deleting a given sub-string
- Check if frequency of all characters can become same by one removal
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if the array can be sorted only if the elements on given positions can be swapped
- Number of swaps to sort when only adjacent swapping allowed
- Generate permutations with only adjacent swaps allowed
- Minimum adjacent swaps required to Sort Binary array
- Minimum number of adjacent swaps to convert a string into its given anagram
- Check if the robot is within the bounds of the grid after given moves
- Count of strings that become equal to one of the two strings after one removal
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Minimum product in a grid of adjacent elements
- Sum of the count of number of adjacent squares in an M X N grid