# Check if a given matrix can be converted to another given matrix by row and column exchanges

Given a matrix startMatrix and another matrix finalMatrix, the task is to check if startMatrix can be converted to finalMatrix by column exchanges and a row exchanges.

Examples:

Input: start[][] = {{1, 2, 3, 4},
{5, 6, 7, 8}
{9, 10, 11, 12},
{13, 14, 15, 16}}

final[][] = {{1, 4, 3, 2},
{5, 8, 7, 6},
{13, 16, 15, 14},
{9, 12, 11, 10}}
Output: Yes
Explanation: Exchanging 2nd and 4th column followed by 4th and 3rd row gives the desired matrix

Input: start[][] = {{1, 2, 3, 4},
{5, 6, 7, 8}
{9, 10, 11, 12},
{13, 14, 15, 16}}

final[][] = {{1, 4, 3, 2},
{5, 6, 7, 8},
{13, 16, 15, 14},
{9, 12, 11, 10}}
Output: No

Approach:
In order to solve this problem we just need to check whether the elements in all rows and columns in startMatrix are preserved in the finalMatrix matrix irrespective of their order. Any violation of this condition will ensure that the finalMatrix matrix can’t be obtained. Traverse a loop through every row and check if all elements of that row in startMatrix is present in a single row in finalMatrix. Then transpose both startMatrix and finalMatrix and repeat the same verification.

Below code is the implementation of the above approach:

 `// Java program to check if a  ` `// given matrix can be converted  ` `// to another given matrix by row ` `// and column exchanges ` ` `  `import` `java.util.*; ` `public` `class` `Solution{ ` ` `  `    ``// Function to get transpose of a matrix ` `    ``static` `int``[][] getTranspose(``int``[][] matrix){ ` `        ``int` `n = matrix.length; ` `        ``int``[][] transpose = ``new` `int``[n][n]; ` `        ``for``(``int` `i=``0``; i> sets = ``new` `ArrayList<>(); ` `        ``ArrayList> setf = ``new` `ArrayList<>(); ` `        ``HashMap map = ``new` `HashMap<>(); ` `         `  `        ``// Creating sets from the initial matrix ` `        ``for``(``int` `i=``0``; i set = ``new` `HashSet(); ` `            ``// Add first element to the set ` `            ``set.add(s[i][``0``]); ` `            ``sets.add(set); ` `            ``// Store the row number in map ` `            ``map.put(s[i][``0``], i); ` `            ``// Add remaining elements to the set ` `            ``for``(``int` `j=``1``; j set = ``new` `HashSet(); ` `             `  `            ``for``(``int` `j=``0``; j

 `# Python 3 program to check if a  ` `# given matrix can be converted  ` `# to another given matrix by row ` `# and column exchanges ` ` `  `# Function to get transpose of a matrix ` `def` `getTranspose(matrix): ` `    ``n ``=` `len``(matrix) ` `    ``transpose ``=` `[[``0` `for` `i ``in` `range``(n)] ``for` `j ``in` `range``(n)] ` `    ``for` `i ``in` `range``(n): ` `        ``for` `j ``in` `range``(n): ` `            ``transpose[j][i] ``=` `matrix[i][j] ` `    ``return` `transpose ` ` `  `# Function to check for row preservation ` `def` `rowEquality(s, f): ` `    ``sets ``=` `[] ` `    ``setf ``=` `[] ` `    ``mp ``=` `{i : ``0` `for` `i ``in` `range``(``100``)} ` `     `  `    ``# Creating sets from the initial matrix ` `    ``for` `i ``in` `range``(``len``(s)): ` `         `  `        ``# Create set for corresponding row ` `        ``st ``=` `set``() ` `         `  `        ``# Add first element to the set ` `        ``st.add(s[i][``0``]) ` `        ``sets.append(st) ` `         `  `        ``# Store the row number in mp ` `        ``mp[s[i][``0``]] ``=` `i ` `         `  `        ``# Add remaining elements to the set ` `        ``for` `j ``in` `range``(``1``, ``len``(s)): ` `            ``st.add(s[i][j]) ` `     `  `    ``# Create sets for final matrix ` `    ``# and check with the initial matrix ` `    ``rowIndex ``=` `-``1` `    ``for` `i ``in` `range``(``len``(f)): ` `        ``rowIndex ``=` `-``1``; ` `        ``st1 ``=` `set``() ` `         `  `        ``for` `j ``in` `range``(``len``(f)): ` `            ``st1.add(f[i][j]) ` `            ``if``(f[i][j] ``in` `mp): ` `                ``rowIndex ``=` `mp[f[i][j]] ` `         `  `        ``setf.append(st1) ` `        ``if``(rowIndex !``=` `-``1` `and` `setf[i] !``=` `           ``sets[rowIndex]): ` `            ``return` `True` `     `  `    ``return` `False` `     `  `#Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``startMatrix ``=` `[[ ``1``, ``2``, ``3``, ``4` `],  ` `                    ``[ ``5``, ``6``, ``7``, ``8` `], ` `                    ``[ ``9``, ``10``, ``11``, ``12` `], ` `                    ``[ ``13``, ``14``, ``15``, ``16` `]] ` `    ``finalMatrix ``=` `[[ ``3``, ``4``, ``1``, ``2``], ` `                        ``[ ``15``, ``16``, ``13``, ``14` `], ` `                        ``[ ``7``, ``8``, ``5``, ``6` `], ` `                        ``[ ``11``, ``12``, ``9``, ``10` `]] ` `     `  `    ``startTranspose ``=` `getTranspose(startMatrix) ` `    ``finalTranspose ``=` `getTranspose(finalMatrix) ` ` `  `    ``if``(rowEquality(startMatrix, finalMatrix) ``and`  `       ``rowEquality(startTranspose, finalTranspose)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# This code is contributed by Samarth `

Output:
```Yes
```

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ipg2016107

Article Tags :
Practice Tags :