Given a binary matrix your task is to find all unique rows of the given matrix in the order of their appearance in the 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 R_{1}: {1 1 0 1} and R_{2}: {1 0 0 1}. 
As R_{1 }first appeared at row-0 and R_{2} appeared at row-1, in the resulting list, R_{1} is kept before R_{2}.
Example 2:
Input:
row = 2, col = 4 
M[][] = {{0 0 0 1}, {0 0 0 1}}
Output: $0 0 0 1 $
Explanation: Above the matrix of size 2x4
looks like
0 0 0 1
0 0 0 1
Only unique row is $0 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 matrix. Do not read input, instead use the arguments given in the function.
Note: The driver code prints the rows "$" separated. You have to just return list of rows, you do not have to worry about printing anything.
Expected Time Complexity: O(row * col)
Expected Auxiliary Space: O(row * col)
Constraints:
1<=row,col<=40
0<=M[][]<=1

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

String algorithms

Starter code: #User function Template for python3

from typing import List


class Solution:
    def uniqueRow(row, col, matrix):
    #complete the function

Solutions:
def uniqueRow(row, col, matrix):
	rowsSet = set()
	result = []
	for currRow in range(row):
		currNum = ''
		for currCol in range(col):
			currNum += str(matrix[currRow * col + currCol]) + ' '
		if currNum not in rowsSet:
			result.append(list(map(int, currNum.split())))
		rowsSet.add(currNum)
	return result


from collections import defaultdict

def uniqueRow(row, col, matrix):
	matLen = len(matrix)
	mat = []
	temp = []
	j = 0
	while j < matLen:
		temp.append(int(matrix[j]))
		j += 1
		if j % col == 0:
			mat.append(temp)
			temp = []
	hashmap = defaultdict(lambda : False)
	ans = []
	for row in mat:
		row = tuple(row)
		if hashmap[row]:
			continue
		else:
			ans.append(row)
			hashmap[row] = True
	return ans


def uniqueRow(row, col, matrix):
	M = []
	for i in range(0, row * col, col):
		M.append(matrix[i:i + col])
	li = []
	for i in range(row):
		if M[i] not in li:
			li.append(M[i])
	return li


def uniqueRow(row, col, matrix):
	x = []
	add = []
	res = []
	for i in range(1, row * col + 1):
		if i % col != 0:
			add.append(matrix[i - 1])
		else:
			add.append(matrix[i - 1])
			x.append(add)
			add = []
	for i in x:
		if i not in res:
			res.append(i)
	return res


def uniqueRow(row, col, matrix):
	d = set()
	k1 = 0
	res1 = []
	for i in range(row):
		s = ''
		for j in range(col):
			s += matrix[k1]
			k1 += 1
		if s not in d:
			d.add(s)
			p = []
			for k in s:
				p.append(k)
			res1.append(p)
	return res1


class Node:

	def __init__(self):
		self.childrens = [None] * 2
		self.is_last = False

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, arr):
		has_inserted = False
		root = self.root
		for d in arr:
			d = int(d)
			if root.childrens[d] is None:
				root.childrens[d] = Node()
				has_inserted = True
			root = root.childrens[d]
		return has_inserted

def uniqueRow(row, col, matrix):
	t = Trie()
	ans = []
	i = 0
	while i <= len(matrix) - col:
		is_unique = t.insert(matrix[i:i + col])
		if is_unique:
			ans.append(matrix[i:i + col])
		i = i + col
	return ans


def uniqueRow(row, col, matrix):
	pool = set()
	a = list()
	for i in range(row):
		start = i * col
		end = start + col
		str1 = ''
		b = []
		string = str1.join(matrix[start:end])
		if string not in pool:
			pool.add(string)
			for i in string:
				b.append(i)
			a.append(b)
	return a


def uniqueRow(row, col, matrix):
	l = []
	t = ''
	for i in range(row * col):
		t += str(matrix[i])
		if i % col == col - 1:
			if t not in l:
				l.append(t)
			t = ''
	return l


from collections import defaultdict

def uniqueRow(row, col, matrix):
	hash = defaultdict(int)
	k = 0
	index = []
	ans = []
	for i in range(row):
		value = 0
		for j in range(col):
			value += int(matrix[k]) * 2 ** (col - j - 1)
			k += 1
		if hash[value] == 0:
			hash[value] += 1
			index.append(i)
	for i in index:
		string = ''.join(matrix[i * col:(i + 1) * col])
		ans.append(string)
	return ans


def uniqueRow(row, col, matrix):
	matrix = [int(i) for i in matrix]
	s = []
	for i in range(row):
		m = matrix[i * col:i * col + col]
		if m not in s:
			s.append(m)
	return [i for i in s]


def uniqueRow(row, col, matrix):
	d = dict()
	ans = []
	for i in range(0, row * col, col):
		x = tuple(matrix[i:i + col])
		if x not in d:
			d[x] = 1
			ans.append(x)
	return ans


def uniqueRow(row, col, matrix):
	myset = set()
	res = []
	for i in range(row):
		temp = ''
		for j in range(col):
			temp += str(matrix[i * col + j])
		if temp not in myset:
			res.append([int(ch) for ch in temp])
			myset.add(temp)
	return res


class Node:

	def __init__(self):
		self.left = None
		self.right = None
		self.eow = 0

def uniqueRow(row, col, matrix):
	treeroot = Node()
	res = []
	temp = ''
	cnum = 0
	for i in range(row):
		root = treeroot
		temp = ''
		for j in range(col):
			if matrix[i * col + j] == '1':
				if root.right == None:
					root.right = Node()
				root = root.right
				temp = temp + '1'
			else:
				if root.left == None:
					root.left = Node()
				root = root.left
				temp = temp + '0'
		if root.eow == 0:
			res.append(temp)
			root.eow = 1
	return res


def uniqueRow(row, col, matrix):
	s = set()
	res = []
	i = 0
	while i < len(matrix):
		st = ''
		j = i
		while j < len(matrix) and j < i + col:
			st += matrix[j]
			j += 1
		i = j
		if st not in s:
			res.append(st)
		s.add(st)
	return res


def uniqueRow(row, col, matrix):
	res = set()
	aux = []
	s = ''
	ini = 0
	r = []
	for i in range(row + 1):
		s = ''.join(matrix[ini:ini + col])
		ini += col
		res.add(s)
		aux.append(s)
	for el in aux:
		if el in res:
			r.append(el)
			res.discard(el)
	r.pop(-1)
	return r


def uniqueRow(row, col, matrix):
	result = []
	for i in range(0, row * col, col):
		bool_value = matrix[i:i + col]
		if bool_value not in result:
			result.append(bool_value)
	return result


def uniqueRow(row, col, matrix):
	res = []
	for i in range(0, row * col, col):
		boolv = matrix[i:i + col]
		if boolv not in res:
			res.append(boolv)
	return res


def uniqueRow(row, col, test_str):
	temp = [test_str[idx:idx + col] for idx in range(0, len(test_str), col)]
	res = list(map(lambda ele: list(ele), temp))
	hmap = []
	for row in res:
		if row not in hmap:
			hmap.append(row)
	return hmap


def uniqueRow(row, col, matrix):
	l = []
	for i in range(1, row + 1):
		i = matrix[i * col - col:i * col]
		i = ''.join(i)
		if i not in l:
			l.append(i)
	return l


def uniqueRow(row, col, matrix):
	s = set()
	l = []
	for i in range(1, row + 1):
		i = matrix[i * col - col:i * col]
		i = ''.join(i)
		if i not in s:
			s.add(i)
			l.append(i)
	return l


def uniqueRow(row, col, matrix):
	l = []
	l2 = []
	k = 0
	for i in range(row):
		l.append(matrix[k:k + col])
		k += col
	for j in l:
		if j not in l2:
			l2.append(j)
	return l2


def uniqueRow(row, col, matrix):
	qw = []
	temp = 0
	aq = []
	dp = []
	for i in range(row):
		for j in range(col):
			dp.append(matrix[temp])
			temp = temp + 1
		aq.append(dp.copy())
		dp = []
	for i in range(len(aq)):
		if aq[i] not in qw:
			qw.append(aq[i])
	return qw


def uniqueRow(row, col, matrix):
	ls = []
	for i in range(row):
		x = matrix[i * col:i * col + col]
		if x not in ls:
			ls.append(x)
	return ls


def uniqueRow(row, col, matrix):
	k = []
	i = 0
	while i < len(matrix):
		if matrix[i:i + col] not in k:
			k.append(matrix[i:i + col])
		i += col
	return k


def uniqueRow(row, col, matrix):
	b = 0
	e = col
	m = []
	for i in range(row):
		m.append(matrix[b:e])
		b = e
		e = b + col
	l = []
	for i in m:
		if i not in l:
			l.append(i)
	return l


class TrieNode:

	def __init__(self):
		self.children = {}
		self.count = 0

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, row):
		cur = self.root
		for c in row:
			if c not in cur.children:
				cur.children[c] = TrieNode()
			cur = cur.children[c]
		cur.count += 1
		return row if cur.count == 1 else None

def uniqueRow(m, n, mat):
	t = Trie()
	ans = []
	i = 0
	while i < len(mat):
		row = []
		j = i
		while j < i + n:
			row.append(mat[j])
			j += 1
		temp = t.insert(row)
		if temp:
			ans.append(temp)
		i += n
	return ans


class trienode:

	def __init__(self):
		self.child = [None] * 2

def uniqueRow(row, col, matrix):

	def insert(root, a):
		isnew = False
		for i in a:
			if root.child[i] == None:
				root.child[i] = trienode()
				isnew = True
			root = root.child[i]
		return isnew

	def count(matrix, row, col):
		r = []
		m = []
		for i in range(row):
			s = []
			for j in range(col):
				s.append(int(matrix[i * col + j]))
			m.append(s)
		root = trienode()
		for i in m:
			if insert(root, i):
				r.append(i)
		return r
	return count(matrix, row, col)


def uniqueRow(row, col, matrix):
	l = []
	for i in range(row):
		s = ''
		for j in range(col):
			s += str(matrix[col * i + j])
		l.append(s)
	k = []
	for x in l:
		if x not in k:
			k.append(x)
	return k


def uniqueRow(row, col, matrix):
	s = set()
	y = []
	for i in range(row):
		x = []
		f = ''
		for j in range(i * col, (i + 1) * col):
			x += [matrix[j]]
			f += matrix[j] + ','
		if f not in s:
			s.add(f)
			y.append(x)
	return y


class Node:

	def __init__(self, data):
		self.data = data
		self.childrens = [None, None]
		self.is_terminal = False

	def __str__(self) -> str:
		return self.data

class Trie:

	def __init__(self):
		self.root = Node(-1)

	def insert(self, root, data):
		is_new_added = False
		for d in data:
			d = int(d)
			if root.childrens[d] is None:
				root.childrens[d] = Node(d)
				is_new_added = True
			root = root.childrens[d]
		root.is_terminal = False
		return is_new_added

	def search(self, root, data):
		print('Searching: ', data)
		for d in data:
			d = int(d)
			if root.childrens[d] is None:
				return False
			root = root.childrens[d]
		return root.is_terminal

def uniqueRow(row, col, matrix):
	t = Trie()
	unique = []
	i = 0
	while i < len(matrix):
		is_unique = t.insert(t.root, matrix[i:i + col])
		if is_unique:
			unique.append(list(matrix[i:i + col]))
		i += col
	return unique


def uniqueRow(row, col, matrix):
	res = []
	for r in range(row):
		curr = []
		for c in range(col):
			curr.append(matrix[r * col + c])
		if curr not in res:
			res.append(curr)
	return res


def uniqueRow(row, col, matrix):
	temporary = []
	final = []
	for idx in range(0, row * col, col):
		final.append(matrix[idx:idx + col])
	for item in final:
		j = 0
		value = 0
		for i in range(len(item) - 1, -1, -1):
			value += int(item[i]) * 2 ** j
			j += 1
		temporary.append(value)
	map = {}
	for (idx, item) in enumerate(temporary):
		if item not in map:
			map[item] = [idx]
		else:
			map[item].append(idx)
	result = []
	map = sorted(map.values())
	for values in map:
		result.append(final[values[0]])
	return result


def uniqueRow(row, col, matrix):
	b = {}
	k = []
	for x in range(row):
		out = ''
		for y in range(col):
			out += matrix[x * col + y]
		if out not in b:
			b[out] = 1
			k.append(out)
	return k


def uniqueRow(row, col, matrix):
	result = []
	for i in range(row):
		s = ''
		for j in range(col):
			s += matrix[i * col + j]
		s.strip()
		if s not in result:
			result.append(s)
	result = [list(i) for i in result]
	return result


def uniqueRow(row, col, matrix):
	l = []
	a = []
	for i in range(row):
		for j in range(col):
			a.append(matrix[col * i + j])
		l.append(a)
		a = []
	ul = []
	for i in l:
		if i not in ul:
			ul.append(i)
	return ul


def uniqueRow(row, col, matrix):
	res = []
	for i in range(0, len(matrix), col):
		str = ''.join(matrix[i:i + col])
		if str not in res:
			res.append(str)
	return res


def uniqueRow(row, col, matrix):
	listt = []
	for i in range(0, len(matrix), col):
		ans = ''.join(matrix[i:i + col])
		if ans not in listt:
			listt.append(ans)
	return listt


def uniqueRow(row, col, matrix):
	ans = set()
	arr = []
	for i in range(0, len(matrix), col):
		temp = []
		j = i
		while j < i + col:
			temp.append(matrix[j])
			j = j + 1
		key = ' '.join(temp)
		if key not in ans:
			ans.add(key)
			arr.append(temp)
	return arr


class TrieNode:

	def __init__(self):
		self.next = dict()
		self.endmark = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		curNode = self.root
		for ch in word:
			if ch not in curNode.next:
				curNode.next[ch] = TrieNode()
			curNode = curNode.next[ch]
		curNode.endmark = True

	def search(self, word):
		curNode = self.root
		for ch in word:
			if ch not in curNode.next:
				return False
			curNode = curNode.next[ch]
		return curNode.endmark

def uniqueRow(row, col, matrix):
	res = []
	trie = Trie()
	(start, end) = (0, col)
	for i in range(row):
		r = matrix[start:end]
		if not trie.search(r):
			res.append(r)
			trie.insert(r)
		(start, end) = (end, end + col)
	return res


def uniqueRow(row, col, matrix):
	unique = []
	for i in range(0, row):
		s = ''
		for j in range(0, col):
			s += matrix[i * col + j]
		if s not in unique:
			unique.append(s)
	return unique


def uniqueRow(row, col, mat):
	res = []
	kmap = {}
	arr = []
	for i in range(0, len(mat), col):
		ans = ''
		for j in range(col):
			ans += str(mat[i + j])
		if ans not in kmap:
			kmap[ans] = i
			arr.append(i)
	for key in arr:
		i = key
		ele = mat[i:i + j + 1]
		res.append(ele)
	return res


class TrieNode:

	def __init__(self):
		self.children = {}

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def isUniqueRow(self, row):
		isUnique = False
		root = self.root
		for x in row:
			if x not in root.children:
				isUnique = True
				root.children[x] = TrieNode()
			root = root.children[x]
		return isUnique

def uniqueRow(r, c, matrix):
	trie = Trie()
	uniqueRows = []
	for i in range(0, r * c, c):
		row = matrix[i:i + c]
		if trie.isUniqueRow(row):
			uniqueRows.append(row)
	return uniqueRows


from collections import OrderedDict

def uniqueRow(row, col, matrix):
	OD = OrderedDict()
	for i in range(row):
		item = ''.join(matrix[i * col:(i + 1) * col])
		if not item in OD:
			OD[item] = 1
	return list(OD.keys())


def uniqueRow(row, col, matrix):
	c = {}
	l = []
	for i in range(0, row * col, col):
		z = tuple(matrix[i:i + col])
		if z not in c:
			c[z] = 1
			l += [matrix[i:i + col]]
	return l


def uniqueRow(row, col, matrix):
	st = set()
	li = list()
	di = dict()
	for i in range(row):
		s = ''.join(matrix[i * col:(i + 1) * col])
		if di.get(s) == None:
			di[s] = 1
			li.append(s)
	return li


def uniqueRow(row, col, matrix):
	visited = []
	for r in range(0, len(matrix), col):
		erow = ''.join(matrix[r:r + col])
		if erow not in visited:
			visited.append(erow)
	return visited


def uniqueRow(row, col, matrix):
	dic = {}
	ans = []
	for i in range(row):
		temp = ''
		for j in range(col):
			temp += matrix[col * i + j]
		if temp not in dic:
			dic[temp] = 1
			ans.append(temp)
	return ans


def uniqueRow(row, col, matrix):
	unique_rows = []
	dp = dict()
	while matrix:
		row = matrix[:col]
		if dp.get(tuple(row)) == None:
			dp[tuple(row)] = 1
			unique_rows.append(row)
		del matrix[:col]
	return unique_rows


def uniqueRow(row, col, matrix):
	d = dict()
	k = 0
	res = []
	for i in range(row):
		l = []
		for j in range(col):
			l.append(matrix[k])
			s1 = ''.join(l)
			k += 1
		if s1 not in d.keys():
			d[s1] = 1
			res.append(l)
	return res


def uniqueRow(row, col, matrix):
	ans = []
	h = {}
	for i in range(row):
		s = ''
		for j in range(i * col, i * col + col):
			s += matrix[j]
		if s not in h:
			h[s] = 1
			ans.append(matrix[i * col:i * col + col])
	return ans


def uniqueRow(row, col, matrix):
	d = {}
	i = 0
	l = []
	for x in range(row):
		s = ''
		r = []
		for y in range(col):
			s += matrix[i]
			r.append(matrix[i])
			i += 1
		if d.get(s) == None:
			d[s] = 1
			l.append(r)
	return l


def uniqueRow(row, col, matrix):
	rec = dict()
	rows = []
	i = 0
	k = col
	for j in range(row):
		temp = matrix[i:k]
		temp1 = ''
		for item in temp:
			temp1 += str(item)
		if temp1 in rec:
			rec[temp1] += 1
		else:
			rec[temp1] = 1
			rows.append(temp)
		i += col
		k += col
	return rows


def uniqueRow(row, col, matrix):
	a = []
	i = 0
	c = []
	while i < row * col:
		if i % col == col - 1:
			c.append(matrix[i])
			if c not in a:
				a.append(c)
				i = i + 1
			else:
				i = i + 1
			c = []
		else:
			c.append(matrix[i])
			i = i + 1
	return a


def uniqueRow(row, col, matrix):
	ans = []
	dp = {}
	for i in range(row):
		ch = ''
		for j in range(i * col, col + i * col):
			ch += matrix[j]
		if ch not in dp:
			dp[ch] = 1
			ans.append(matrix[i * col:col + i * col])
	return ans


import io
L = 2

class Node:

	def __init__(self):
		self.children = [None for _ in range(L)]

	@staticmethod
	def _to_idx(value):
		return ord(value) - ord('0')

def uniqueRow(row, col, matrix):
	current = root = Node()
	results = []
	new = False
	line = []
	for pos in range(row * col):
		value = matrix[pos]
		line.append(value)
		idx = Node._to_idx(value)
		if current.children[idx] is None:
			new = True
			current.children[idx] = Node()
		current = current.children[idx]
		if (pos + 1) % col == 0:
			current = root
			if new:
				results.append(line)
			line = []
			new = False
	return results


def uniqueRow(row, col, matrix):
	l = []
	res = []
	k = 0
	c = [0 for i in range(row)]
	for i in range(row):
		l1 = []
		for j in range(col):
			l1.append(matrix[k])
			k += 1
		l.append(l1)
	for i in l:
		if i not in res:
			res.append(i)
	return res


def uniqueRow(row, col, matrix):
	l1 = []
	l = []
	r = 0
	for i in range(row):
		l1.append(matrix[r:r + col])
		r += col
	for j in l1:
		if j not in l:
			l.append(j)
	return l


def uniqueRow(row, col, matrix):
	d = set()
	count = 0
	for i in range(0, row):
		st = ''
		for j in range(0, col):
			st += str(matrix[count])
			count += 1
		d.add(st)
	l = []
	for i in range(0, row * col - col + 1, col):
		st = ''
		for j in matrix[i:i + col]:
			st += str(j)
		if st in d:
			l.append(matrix[i:i + col])
			d.remove(st)
	return l


def uniqueRow(row, col, matrix):
	dict1 = {}
	ans = []
	c1 = 0
	c2 = col
	i = 0
	while i < row:
		if tuple(matrix[c1:c2]) in dict1:
			dict1[tuple(matrix[c1:c2])] += 1
		else:
			dict1[tuple(matrix[c1:c2])] = 1
		i += 1
		c1 = c2
		c2 += col
	c1 = 0
	c2 = col
	for i in range(row):
		if dict1[tuple(matrix[c1:c2])] >= 1:
			ans.append(matrix[c1:c2])
			dict1[tuple(matrix[c1:c2])] = 0
		c1 = c2
		c2 += col
	return ans


def uniqueRow(row, col, matrix):
	ans = [matrix[:col]]
	for i in range(col, len(matrix), col):
		if matrix[i:i + col] not in ans:
			ans.append(matrix[i:i + col])
	return ans


def uniqueRow(row, col, matrix):
	mydict = {}
	ans = []
	i = 0
	while i < len(matrix):
		row = matrix[i:i + col]
		if tuple(row) not in mydict:
			mydict[tuple(row)] = 0
			ans.append(row)
		i += col
	return ans


def uniqueRow(row, col, matrix):
	M = set()
	ans = []
	for r in range(row):
		rv = tuple((matrix[r * col + j] for j in range(col)))
		if rv not in M:
			M.add(rv)
			ans.append(rv)
	return ans


def uniqueRow(row, col, matrix):
	a = []
	for j in range(0, row * col, col):
		boolv = matrix[j:j + col]
		if boolv not in a:
			a.append(boolv)
	return a


def uniqueRow(row, col, matrix):
	seen = []
	for i in range(0, row * col, col):
		if not tuple(matrix[i:i + col]) in seen:
			seen.append(tuple(matrix[i:i + col]))
	return seen


def uniqueRow(row, col, matrix):
	m = []
	for i in range(row):
		out = []
		for j in range(col):
			out.append(matrix[col * i + j])
		m.append(out)
	res = []
	for row in m:
		if row not in res:
			res.append(row)
	return res


def uniqueRow(row, col, matrix):
	lst1 = []
	lst2 = []
	for i in range(0, len(matrix), col):
		lst = list(matrix[i:i + col])
		lst1.append(lst)
	for i in lst1:
		if i not in lst2:
			lst2.append(i)
	return lst2


def uniqueRow(row, col, matrix):
	di = {}
	ans = []
	k = 0
	temp = []
	for i in range(len(matrix)):
		temp.append(int(matrix[i]))
		k += 1
		if k == col:
			k = 0
			ans.append(temp)
			temp = []
	final = []
	te = ''
	pi = {}
	for i in ans:
		for j in i:
			te += str(j)
		if te not in pi:
			pi[te] = 1
			final.append(i)
		te = ''
	return final


def uniqueRow(row, col, matrix):
	a = []
	c = 0
	temp = []
	for i in matrix:
		if c != col:
			temp.append(i)
			c += 1
		if c == col:
			a.append(temp)
			temp = []
			c = 0
	s = set()
	l = []
	for row in a:
		e = ''.join(row)
		if e not in s:
			l.append(row)
			s.add(e)
	return l


def uniqueRow(row, col, matrix):
	mul = row * col
	new_lst = []
	for i in range(row):
		for j in range(0, mul, col):
			sub = matrix[j:j + col]
			if sub not in new_lst:
				new_lst.append(sub)
	return new_lst


def uniqueRow(row, col, matrix):
	m = matrix
	mul = row * col
	ans = []
	for i in range(1):
		for j in range(0, mul, col):
			sub = m[j:j + col]
			if sub not in ans:
				ans.append(sub)
	return ans