Given N and a N*N matrix containing 0’s and 1’s. Group all the row numbers (starting index 0) which are having 1’s at same position.
Example 1:
Input:
N=4
matrix= [0010
            0100
            0010
            0000]
Output:
0 1
Explanation:
In the above test case, 1st and 3rd row have
the 1's at same column,i.e., 2(starting index 
from 0) so group first and third row. And print 
the index of first row, i.e., 0 (starting index 
from 0).
For second row, as it is the only row with same 
indexing of 1's so print it's index , i.e., 1.
For forth row, 1 is not present in this row so 
didn't print anything.
Example 2:
Input:
N=1
matrix = [0]
Output:
-1
Explanation:
There is no row containing 1.
 
Your task:
You don't need to read input or print anything. Your task is to complete the function groupRows(), which takes  the matrix and an integer N as input parameters and returns a list of row numbers after grouping. If there is no row containing 1, return -1.
 
Expected Time Complexity: O(n*n)
Expected Auxiliary Space: O(n*n)
 
Constraints:
1<=N<=20

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

    def groupRows(self, M , n):

        #code here

Solutions:
class Solution:

	def groupRows(self, M, n):
		dic = {}
		ans = []
		row = 0
		for i in M:
			num = 0
			for j in range(n - 1, -1, -1):
				num += int(i[j]) * 2 ** j
			if num > 0:
				if num not in dic:
					dic[num] = row
				row += 1
		for i in dic.keys():
			ans.append(dic[i])
		ans.sort()
		if len(ans) == 0:
			return [-1]
		return ans


class Solution:

	def groupRows(self, M, n):
		setti = set()
		for item in M:
			if '1' in item:
				setti.add(item)
		return [i for i in range(len(setti))] if setti else [-1]


class Solution:

	def groupRows(self, M, n):
		l = []
		s = {}
		k = 0
		for i in range(n):
			e = ''.join(M[i])
			if '1' in e:
				if e not in s:
					s[e] = 1
					l.append(k)
					k += 1
		if len(l) == 0:
			return [-1]
		return l


class Solution:

	def groupRows(self, M, n):
		s = set()
		arr = []
		for i in range(n):
			p = ''.join(M[i])
			r = int(p, 10)
			if r > 0:
				arr.append(r)
		if len(arr) == 0:
			return [-1]
		ans = []
		for i in range(len(arr)):
			if arr[i] not in s and arr[i] != 0:
				s.add(arr[i])
				ans.append(i)
		return ans