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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = 3
n = 3
s = [[ 1, 0, 1 ],
     [ 0, 0, 0 ],
     [ 1, 0, 0 ]]
  
uniqueRows(s, m, n)
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

Yes


My Personal Notes arrow_drop_up

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.