-
Notifications
You must be signed in to change notification settings - Fork 5
/
UniqueRowsInMatrix.java
104 lines (99 loc) · 2.53 KB
/
UniqueRowsInMatrix.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*https://practice.geeksforgeeks.org/problems/unique-rows-in-boolean-matrix/1*/
class TrieNode
{
TrieNode[] arr;
boolean isEndOfRow;
TrieNode()
{
arr = new TrieNode[2];
isEndOfRow = false;
}
}
class Trie
{
TrieNode root;
Trie()
{
root = new TrieNode();
}
public boolean isPresent(int[] row)
{
boolean result = true;
TrieNode temp = root;
for (int i = 0; i < row.length; ++i)
{
if (temp.arr[row[i]] == null)
{
result = false;
temp.arr[row[i]] = new TrieNode();
}
temp = temp.arr[row[i]];
}
if (result && temp.isEndOfRow) result = true; else result = false;
temp.isEndOfRow = true;
return result;
}
}
class GfG
{
public static ArrayList<ArrayList<Integer>> uniqueRow(int a[][],int r, int c)
{
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
Trie trie = new Trie();
trie.isPresent(a[0]);
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < a[0].length; ++i)
temp.add(a[0][i]);
result.add(temp);
for (int i = 1; i < a.length; ++i)
{
if (!trie.isPresent(a[i]))
{
temp = new ArrayList<Integer>();
for (int j = 0; j < a[i].length; ++j)
temp.add(a[i][j]);
result.add(temp);
}
}
return result;
}
}
/*Pratik's Code*/
class GfG
{
public static ArrayList<ArrayList<Integer>> uniqueRow(int a[][],int r, int c)
{
//add code here.
TrieNode root = new TrieNode();
ArrayList<ArrayList<Integer>> aList = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<r;i++)
{
TrieNode temp = root;
ArrayList<Integer> al = new ArrayList<Integer>();
for(int j=0;j<c;j++)
{
al.add(a[i][j]);
if(temp.arr[a[i][j]]==null)
{
TrieNode newNode = new TrieNode();
temp.arr[a[i][j]] = newNode;
}
temp = temp.arr[a[i][j]];
}
if(temp.isEndOfRow==false)
{
aList.add(al);
temp.isEndOfRow = true;
}
}
return aList;
}
}
class TrieNode
{
TrieNode arr[] = new TrieNode[2];
boolean isEndOfRow;
TrieNode(){
isEndOfRow = false;
}
}