Problem Statement

Given a binary matrix your task is to find all unique rows of the given matrix.

Example 1:

Input:
row = 3, col = 4
M[][] = {{1 1 0 1},{1 0 0 1},{1 1 0 1}}
Output:1 1 0 1 $1 0 0 1 $
Explanation:Above the matrix of size 3x4
looks like
1 1 0 1
1 0 0 1
1 1 0 1
The two unique rows are 1 1 0 1 and
1 0 0 1 .

**Your Task:**You only need to implement the given function uniqueRow(). The function takes three arguments the first argument is a matrix M and the next two arguments are row and col denoting the rows and columns of the matrix. The function should return the list of the unique row of the martrix. Do not read input, instead use the arguments given in the function.

Note: The drivers code print the rows "$" separated.

Expected Time Complexity: O(row * col)

Expected Auxiliary Space: O(row * col)

**Constraints:**1<=row,col<=400<=M[][]<=1

Problem Link

Unique rows in boolean matrix | Practice | GeeksforGeeks

Code

struct TrieNode {
    int data;
    
    bool rowend;
    TrieNode *child[2];
    
    TrieNode (char d)
    {
        data = d;
        rowend = false;
        **for(int i=0;i<2;i++)
        child[i] = NULL;**
    }
    
};
TrieNode *root = NULL;

void initialize()
{
    root = new TrieNode('/');
}

bool insert(vector<int> v)
{
    TrieNode *curr = root;
    
    for(int i=0;i<v.size();i++)
    {
        **int index = v[i];
        
        if(!curr->child[index])
        curr->child[index] = new TrieNode(v[i]);**
        
        curr = curr->child[index];
    }
    if(curr->rowend)
    return false;
    else
    {
        return curr->rowend = true;
    }
    
}

vector<vector<int>> uniqueRow(int M[MAX][MAX],int row,int col)
{
    
    vector<vector<int>> res;
    initialize();
    
    for(int i=0;i<row;i++)
    {
        vector<int> v(col);
        for(int j=0;j<col;j++)
        {
            v[j] = M[i][j];
        }
        if(insert(v))
        res.push_back(v);
    }
    
   
    return res;
}