Check if a grid can become row | C ++ 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 . ; Driver code ; int len = 5 ; Length of strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } int main ( ) { vector < string > v = { " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " } ; check ( v , len ) ? cout << " Yes " : cout << " No " ; return 0 ; }