
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if matrix can be converted to another matrix by transposing square sub-matrices in Python
Suppose we have two N X M called mat1 and mat2. In an operation, we can transpose any square sub-matrix in mat1. We have to check whether we can get mat2 from mat1 or not by performing given operation.
So, if the input is like
5 | 6 | 7 |
1 | 2 | 3 |
6 | 8 | 9 |
5 | 6 | 2 |
1 | 7 | 3 |
6 | 8 | 9 |
then the output will be True, because if we get transpose of top right sub-matrix of size 2x2 of mat1, we will get mat2.
To solve this, we will follow these steps −
- row := row count of matrices
- column := column count of matrices
- for i in range 0 to row - 1, do
- temp1 := a new list, temp2 := a new list
- r := i, col := 0
- while r >= 0 and col < column, do
- insert mat1[r, col] into temp1
- insert mat2[r, col] into temp2
- r := r - 1, col := col + 1
- sort the list temp1 and temp2
- for i in range 0 to size of temp1 - 1, do
- if temp1[i] is not same as temp2[i], then
- return False
- if temp1[i] is not same as temp2[i], then
- for j in range 1 to column - 1, do
- temp1 := a new list, temp2 := a new list
- r := row - 1, col := j
- while r >= 0 and col < column, do
- insert mat1[r, col] into temp1
- insert mat2[r, col] into temp2
- r := r - 1, col := col + 1
- sort the list temp1 and temp2
- for i in range 0 to size of temp1 - 1, do
- if temp1[i] is not same as temp2[i], then
- return False
- if temp1[i] is not same as temp2[i], then
- return True
Example
Let us see the following implementation to get better understanding −
def solve(mat1, mat2): row = len(mat1) column = len(mat1[0]) for i in range(row): temp1 = [] temp2 = [] r = i col = 0 while r >= 0 and col < column: temp1.append(mat1[r][col]) temp2.append(mat2[r][col]) r -= 1 col += 1 temp1.sort() temp2.sort() for i in range(len(temp1)): if temp1[i] != temp2[i]: return False for j in range(1, column): temp1 = [] temp2 = [] r = row - 1 col = j while r >= 0 and col < column: temp1.append(mat1[r][col]) temp2.append(mat2[r][col]) r -= 1 col += 1 temp1.sort() temp2.sort() for i in range(len(temp1)): if temp1[i] != temp2[i]: return False return True mat1 = [ [5, 6, 7], [1, 2, 3], [6, 8, 9]] mat2 = [ [5, 6, 2], [1, 7, 3], [6, 8, 9]] print(solve(mat1, mat2))
Input
[ [5, 6, 7], [1, 2, 3], [6, 8, 9]], [ [5, 6, 2], [1, 7, 3], [6, 8, 9]]
Output
True
- Related Articles
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Program to check one string can be converted to another by removing one element in Python
- Swift program to check if a given square matrix is an Identity Matrix
- Program to check whether one point can be converted to another or not in Python
- How to check if a string can be converted to float in Python?
- How to Check if the Matrix is a Magic Square in Java?
- Program to rotate square matrix by 90 degrees counterclockwise in Python
- Program to check words can be found in matrix character board or not in Python
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Rotating a 2-D (transposing a matrix) in JavaScript
- Check if a string can be obtained by rotating another string 2 places in Python
- C++ Program to Multiply two Matrices by Passing Matrix to Function
- Check if a string can be repeated to make another string in Python
- Check if a matrix is symmetric using Python

Advertisements