-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_UniqueRowsInBooleanMatrix.cpp
58 lines (53 loc) · 1.21 KB
/
GFG_UniqueRowsInBooleanMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
https://practice.geeksforgeeks.org/problems/unique-rows-in-boolean-matrix/1
Unique rows in boolean matrix
*/
struct TrieNode{
TrieNode* children[2];
bool isEnd;
}*root;
vector<vector<int>> ans;
int coln;
void insertIntoTrie(int mat[MAX])
{
TrieNode* ptr = root;
for(int c=0; c<coln; c++)
{
int ele = mat[c];
if(ptr->children[ele] == nullptr)
ptr->children[ele] = new TrieNode();
ptr = ptr->children[ele];
}
if(ptr->isEnd == false)
{
ptr->isEnd = true;
ans.push_back(vector<int>(mat, mat+coln));
}
}
/*You are required to complete this function*/
vector<vector<int>> uniqueRow(int M[MAX][MAX],int row,int col)
{
coln = col;
root = new TrieNode();
ans.clear();
for(int r=0; r<row; r++)
insertIntoTrie(M[r]);
return ans;
}
// Approach 2
/*You are required to complete this function*/
vector<vector<int>> uniqueRow(int M[MAX][MAX],int row,int col)
{
vector<vector<int>> ans;
set<vector<int>> s;
for(int r=0; r<row; r++)
{
vector<int> v(M[r], M[r] + col);
if(s.find(v) == s.end())
{
ans.push_back(v);
s.insert(v);
}
}
return ans;
}