![]() |
VOOZH | about |
Given a binary matrix, print all unique rows of the given matrix.
Example:
Input:
{0, 1, 0, 0, 1}
{1, 0, 1, 1, 0}
{0, 1, 0, 0, 1}
{1, 1, 1, 0, 0}
Output:
0 1 0 0 1
1 0 1 1 0
1 1 1 0 0
Explanation:
The rows are r1={0, 1, 0, 0, 1},
r2={1, 0, 1, 1, 0}, r3={0, 1, 0, 0, 1},
r4={1, 1, 1, 0, 0}, As r1 = r3, remove r3
and print the other rows.
Input:
{0, 1, 0}
{1, 0, 1}
{0, 1, 0}
Output:
0 1 0
1 0 1
Explanation:
The rows are r1={0, 1, 0},
r2={1, 0, 1}, r3={0, 1, 0} As r1 = r3,
remove r3 and print the other rows.
: This method explains the simple approach towards solving the above problem.
Approach: A simple approach would be to check each row with all processed rows. Print the first row. Now, starting from the second row, for each row, compare the row with already processed rows. If the row matches with any of the processed rows, skip it else print it.
Algorithm:
- Traverse the matrix row-wise
- For each row check if there is any similar row less than the current index.
- If any two rows are similar then do not print the row.
- Else print the row.
Implementation:
0 1 0 0 1 1 0 1 1 0 1 0 1 0 0
Complexity Analysis:
: This method uses Binary Search Tree to solve the above operation. The Binary Search Tree is a node-based binary tree data structure which has the following properties:
The above properties of Binary Search Tree provide ordering among keys so that the operations like search, minimum and maximum can be done fast. If there is no order, then we may have to compare every key to search a given key.
Approach: The process must begin from finding the decimal equivalent of each row and inserting them into a BST. As we know, each node of the BST will contain two fields, one field for the decimal value, other for row number. One must not insert a node if it is duplicated. Finally, traverse the BST and print the corresponding rows.
Algorithm:
- Create a BST in which no duplicate elements can be stored. Create a function to convert a row into decimal and to convert the decimal value into binary array.
- Traverse through the matrix and insert the row into the BST.
- Traverse the BST (inorder traversal) and convert the decimal into binary array and print it.
Implementation:
1 0 1 0 0 1 0 1 1 0 0 1 0 0 1
Complexity Analysis:
: This method uses Trie data structure to solve the above problem. Trie is an efficient information retrieval data structure. Using Trie, search complexities can be brought to an optimal limit (key length). If we store keys in the binary search tree, a well-balanced BST will need time proportional to M * log N, where M is maximum string length and N is the number of keys in the tree. Using Trie, we can search the key in O(M) time. However, the penalty is on Trie storage requirements.
Note: This method will lead to Integer Overflow if the number of columns is large.
Approach:
Since the matrix is boolean, a variant of Trie data structure can be used where each node will be having two children one for 0 and other for 1. Insert each row in the Trie. If the row is already there, don't print the row. If the row is not there in Trie, insert it in Trie and print it.Algorithm:
- Create a Trie where rows can be stored.
- Traverse through the matrix and insert the row into the Trie.
- Trie cannot store duplicate entries so the duplicates will be removed
- Traverse the Trie and print the rows.
Implementation:
0 1 0 0 1 1 0 1 1 0 1 0 1 0 0
Complexity Analysis:
: This method uses HashSet data structure to solve the above problem. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class offers constant time performance for the basic operations like add, remove, contains and size assuming the hash function disperses the elements properly among the buckets.
Approach: In this method convert the whole row into a single String and then if check it is already present in the HashSet or not. If the row is present then we will leave it otherwise we will print unique row and add it to HashSet.
Algorithm:
- Create a HashSet where rows can be stored as a String.
- Traverse through the matrix and insert the row as String into the HashSet.
- HashSet cannot store duplicate entries so the duplicates will be removed
- Traverse the HashSet and print the rows.
Implementation:
01001 10110 11100
Complexity Analysis: