![]() |
VOOZH | about |
Given a binary matrix mat[][], print the rows which are duplicates of rows that are already present in the matrix.
Examples:
Input: mat[][] = [[1, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 0, 1]]
Output: [4, 5, 6]
Explanation: Row at index 4 is same as 0. Rows at indexes 5 and 6 are same as index 1
Table of Content
For each row, check if it already exists by scanning all previous rows.
4 5 6
A Trie is used to efficiently store rows of the binary matrix since each row contains only 0 and 1. Each row is inserted as a path in the Trie. If the path already exists and the final node is marked as a leaf, the row is identified as a duplicate, otherwise the row is inserted and the end node is marked.
4 5 6
Use a hash set to store string representations of each row, allowing for quick lookup and comparison. By iterating through the rows and concatenating their elements into strings, then checks for duplicate strings in the hash set. If a duplicate is found, the index of the corresponding row is added to a result array.
4 5 6