- Related Questions & Answers
- Goat Latin in Python
- Guess Nearest Square Root in Python
- Square list of elements in sorted form in Python
- Check if given number is perfect square in Python
- Magic Square
- Program to print Square inside a Square in C
- How to calculate square root of a number in Python?
- How to find Square root of complex numbers in Python?
- Count square and non-square numbers before n in C++
- Maximal Square in C++
- Difference between sum of square and square of sum in JavaScript
- How to perform square root without using math module in Python?
- Program to rotate square matrix by 90 degrees counterclockwise in Python
- Program to find number of square submatrices with 1 in python
- Mid-Square hashing in C++.

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an N x N matrix of letters matrix, we have to check whether there are exactly N different letters that appear in the matrix and each letter is present exactly once in each row and exactly once in each column.

So, if the input is like

x | y | z |

z | x | y |

y | z | x |

Then the output will be True, as there are 3 different letters and each letter is present exactly once in each row and column.

To solve this, we will follow these steps −

- ch:= a set of all elements present in the matrix
- m:= transpose of mat
- for each row i from mat and row j from m, do
- for each c in ch, do
- if c not in i or c not in j, then
- return False

- if c not in i or c not in j, then

- for each c in ch, do
- return True

Let us see the following implementation to get better understanding −

class Solution: def solve(self, mat): ch=set(j for i in mat for j in i) m=zip(*mat) for i,j in zip(mat,m): for c in ch: if c not in i or c not in j: return False return True ob = Solution() mat = [["x", "y", "z"],["z", "x", "y"], ["y", "z", "x"]] print(ob.solve(mat))

[["x", "y", "z"], ["z", "x", "y"], ["y", "z", "x"]]

True

Advertisements