Check if the rows of a binary matrix can be made unique by removing a single column
Given a binary matrix mat[][] of size M * N. The task is to check whether the row of the matrix will be unique after deleting a column from the matrix.
Example:
Input: mat[][] = {
{1 0 1},
{0 0 0},
{1 0 0}
}
Output: Yes
After deleting 2nd column each row of matrix become unique.
Input:mat[][] = {
{1 0},
{1 0}
}
Output: No
Approach:
- Take matrix as an array of string and each row consider as single string.
- Initialise a variable count = 0 to count duplicate rows in matrix.
- Now take two nested loop outer loop from i = 0 to str.length() and inner loop from j = 0 to i and for each index check if str[i] == str[j] then increment count by 1 and break the loop.
- Now check if count>= 1 then print No else print Yes.
Below is implementation of Above Approach:
C++
// C++ program to check whether the // Rows of Binary Matrix become unique // After Deleting a column. #include <bits/stdc++.h> using namespace std; // Function to check whether rows of // Binary matrix become unique after // Deleting a column of from matrix. int uniqueRows(string s[], int m, int n) { // Initialize variable count that // stores the count of duplicate rows. int i, j, count = 0; // Take two nested loop and // check weather rows already // get seen then increment // count by 1 then break the loop. for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { if (s[i] == s[j]) { count++; break; } } } // Check if count>=1 then print No // Else print Yes. if (count >= 1) { cout << "No" << endl; } else { cout << "Yes" << endl; } return 0; } // Driver code. int main() { int m = 3, n = 3; string s[] = { { 1, 0, 1 }, { 0, 0, 0 }, { 1, 0, 0 } }; // Function calling uniqueRows(s, m, n); return 0; } |
chevron_right
filter_none
Java
// Java program to check whether the // Rows of Binary Matrix become unique // After Deleting a column. class GFG { // Function to check whether rows of // Binary matrix become unique after // Deleting a column of from matrix. static int uniqueRows(int [][]s, int m, int n) { // Initialize variable count that // stores the count of duplicate rows. int i, j, count = 0; // Take two nested loop and // check weather rows already // get seen then increment // count by 1 then break the loop. for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { if (s[i] == s[j]) { count++; break; } } } // Check if count>=1 then print No // Else print Yes. if (count >= 1) System.out.println("No"); else System.out.println("Yes"); return 0; } // Driver code. public static void main(String[] args) { int m = 3, n = 3; int s[][] = { { 1, 0, 1 }, { 0, 0, 0 }, { 1, 0, 0 } }; // Function calling uniqueRows(s, m, n); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Python3
# Python3 program to check whether the # Rows of Binary Matrix become unique # After Deleting a column. # Function to check whether rows of # Binary matrix become unique after # Deleting a column of from matrix. def uniqueRows(s, m, n): # Initialize variable count that # stores the count of duplicate rows. i, j, count = 0, 0, 0 # Take two nested loop and # check weather rows already # get seen then increment # count by 1 then break the loop. for i in range(n): for j in range(i): if (s[i] == s[j]): count += 1 break # Check if count>=1 then prNo # Else prYes. if (count >= 1): print("No" ) else: print("Yes") # Driver code. m = 3n = 3s = [[ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 0, 0 ]] uniqueRows(s, m, n) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# iprogram to check whether the // Rows of Binary Matrix become unique // After Deleting a column. using System; class GFG { // Function to check whether rows of // Binary matrix become unique after // Deleting a column of from matrix. static int uniqueRows(string []s, int m, int n) { // Initialize variable count that // stores the count of duplicate rows. int i, j, count = 0; // Take two nested loop and // check weather rows already // get seen then increment // count by 1 then break the loop. for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { if (s[i] == s[j]) { count++; break; } } } // Check if count>=1 then print No // Else print Yes. if (count >= 1) Console.WriteLine("No"); else Console.WriteLine("Yes"); return 0; } // Driver code. public static void Main(String[] args) { int m = 3, n = 3; string []s = { "101","000", "100"}; // Function calling uniqueRows(s, m, n); } } // This code is contributed by Princi Singh |
chevron_right
filter_none
Output:
Yes
Recommended Posts:
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Print unique rows in a given boolean matrix
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Removing row or column wise duplicates from matrix of characters
- Find duplicate rows in a binary matrix
- Check if all rows of a matrix are circular rotations of each other
- Python Counter| Find duplicate rows in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Unique cells in a binary matrix
- Check if sums of i-th row and i-th column are same in matrix
- Find if a binary matrix exists with given row and column sums
- Check if the given matrix is increasing row and column wise
- Change if all bits can be made same by single flip
- Program to check if a matrix is Binary matrix or not
- Check horizontal and vertical symmetry in binary matrix
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.

