Given a 2-D integer array A of order NXN, consisting of only zeroes and ones, find the row or column whose all elements form a palindrome. If more than one palindrome is found, the palindromes found in rows will have priority over the ones found in columns and within rows and columns, the one with lesser index gets the highest priority. Print the palindrome with the highest priority along with it's index and indication of it being a row or column.
 
Example 1:
Input:
N = 3
A[][] = [[1, 0, 0], [0, 1, 0], [1, 1, 0]]
Output: 1 R
Explanation:
In the first test case, 0-1-0 is a palindrome 
occuring in a row having index 1.
 
Example 2:
Input:
N = 2
A[][] = [[1, 0], [1, 0]]
Output: 0 C
Explanation:
1-1 occurs before 0-0 in the 0th column.
And there is no palindrome in the two rows.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function pattern() which takes the integer N and 2-d array A[][] as input parameters and returns the First, the index of either row or column(0 based indexing) , followed by either of the two characters 'R' or 'C', representing whether the palindrome was found in a row or column. All the characters return as output should be space separated. If no palindrome is found, return -1 as string.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=50
A[i][j]=0 or 1, where i and j denote row and column indices of the matrix respectively.

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3

class Solution:
    def pattern (self, a, N):
        # code here


Solutions:
class Solution:

	def pattern(self, a, N):

		def myfun(m):
			k = len(m)
			s = ''
			for i in range(N):
				s += str(m[i])
			s2 = s[::-1]
			return s2 == s
		for i in range(N):
			if myfun(a[i]):
				return str(i) + ' R'
		for i in range(N):
			for j in range(i, N):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		for i in range(N):
			if myfun(a[i]):
				return str(i) + ' C'
		return '-1'


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			l = 0
			r = N - 1
			while l < r:
				if a[i][l] == a[i][r]:
					l += 1
					r -= 1
				else:
					break
			else:
				return str(i) + ' R'
		for i in range(N):
			l = 0
			r = N - 1
			while l < r:
				if a[l][i] == a[r][i]:
					l += 1
					r -= 1
				else:
					break
			else:
				return str(i) + ' C'
		return '-1'


class Solution:

	def pattern(self, a, N):
		for i in a:
			if i == i[::-1]:
				return str(a.index(i)) + ' ' + 'R'
		for i in range(N):
			for j in range(i + 1, N):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		for i in range(N):
			if a[i] == a[i][::-1]:
				return str(i) + ' ' + 'C'
		return -1


class Solution:

	def myfun(self, m):
		s = ''
		for i in range(len(m)):
			s += str(m[i])
		if s == s[::-1]:
			return True
		else:
			return False

	def pattern(self, a, N):
		N = len(a)
		M = len(a[0])
		for i in range(N):
			if self.myfun(a[i]):
				return str(i) + ' ' + 'R'
		for i in range(N):
			for j in range(i + 1, M):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		N = len(a)
		M = len(a[0])
		for i in range(N):
			if self.myfun(a[i]):
				return str(i) + ' ' + 'C'
		return -1


class Solution:

	def pattern(self, a, N):
		ind = 0
		r = []
		r_find = 0
		c_find = 0
		c_list = []
		c_list_rev = []
		for i in a:
			for j in reversed(range(N)):
				r.append(i[j])
			if i == r:
				r_find = 1
				return str(ind) + ' R'
			r.clear()
			ind = ind + 1
		ind = 0
		c = 0
		if r_find == 0:
			while c < N:
				for i in a:
					c_list.append(i[ind])
				for j in reversed(range(N)):
					c_list_rev.append(c_list[j])
				if c_list == c_list_rev:
					c_find = 1
					return str(c) + ' C'
				c_list.clear()
				c_list_rev.clear()
				c = c + 1
				ind = ind + 1
		return -1


class Solution:

	def pattern(self, a, N):
		col = list(zip(*a))
		row = 0
		for r in a:
			if r == r[::-1]:
				return '{} {}'.format(row, 'R')
			row += 1
		for c in range(N):
			if list(col[c]) == list(col[c])[::-1]:
				return '{} {}'.format(c, 'C')
		return '{}'.format('-1')


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			if a[i] == a[i][::-1]:
				return str(i) + ' R'
		for (i, col) in enumerate(zip(*a)):
			if col == col[::-1]:
				return str(i) + ' C'
		return '-1'


class Solution:

	def pattern(self, arr, N):
		for i in range(N):
			s = 0
			e = N - 1
			while s < e:
				if arr[i][e] != arr[i][s]:
					break
				e -= 1
				s += 1
			else:
				return str(i) + ' R'
		for i in range(N):
			s = 0
			e = N - 1
			while s < e:
				if arr[e][i] != arr[s][i]:
					break
				e -= 1
				s += 1
			else:
				return str(i) + ' C'
		return -1


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			flag = True
			for j in range(N):
				if a[i][j] == a[i][N - j - 1]:
					continue
				else:
					flag = False
					break
			if flag:
				return '{0} {1}'.format(i, 'R')
		for i in range(N):
			flag = True
			for j in range(N):
				if a[j][i] == a[N - j - 1][i]:
					continue
				else:
					flag = False
					break
			if flag:
				return '{0} {1}'.format(i, 'C')
		return '-1'


class Solution:

	def pattern(self, a, N):
		r = ''
		c = -1
		for i in a:
			c += 1
			for j in i:
				r += str(j)
			rev = ''
			for k in r:
				rev = k + rev
			if rev == r:
				return str(c) + ' R'
			r = ''
		x = ''
		for j in range(N):
			for i in a:
				x += str(i[j])
			w = ''
			for k in x:
				w = k + w
			if w == x:
				return str(j) + ' C'
			x = ''
		return '-1'


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			isRow = True
			for j in range(N // 2):
				if a[i][j] != a[i][N - j - 1]:
					isRow = False
					break
			if isRow:
				return str(i) + ' R'
		for i in range(N):
			isCol = True
			for j in range(N // 2):
				if a[j][i] != a[N - j - 1][i]:
					isCol = False
					break
			if isCol:
				return str(i) + ' C'
		return -1


class Solution:

	def pattern(self, a, N):
		s = ''
		for i in range(N):
			s = ''.join(list(map(str, a[i])))
			if s == s[::-1]:
				return str(i) + ' ' + 'R'
		for i in range(N):
			s = ''
			for j in range(N):
				s += str(a[j][i])
			if s == s[::-1]:
				return str(i) + ' ' + 'C'
		return -1


class Solution:

	def pattern(self, a, N):
		for x in range(N):
			(i, j) = (0, N - 1)
			while i < j:
				if a[x][i] != a[x][j]:
					break
				i += 1
				j -= 1
			if i >= j:
				return str(x) + ' ' + 'R'
		for x in range(N):
			(i, j) = (0, N - 1)
			while i < j:
				if a[i][x] != a[j][x]:
					break
				i += 1
				j -= 1
			if i >= j:
				return str(x) + ' ' + 'C'
		return -1


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			if a[i] == a[i][::-1]:
				return str(i) + ' R'
		for i in range(N):
			s = ''
			for j in range(N):
				s += str(a[j][i])
			if s == s[::-1]:
				return str(i) + ' C'
		return -1
if __name__ == '__main__':
	ob = Solution()
	t = int(input())
	for _ in range(t):
		N = int(input())
		L = list(map(int, input().split()))
		a = list()
		c = 0
		for i in range(0, N):
			X = list()
			for j in range(0, N):
				X.append(L[c])
				c += 1
			a.append(X)
		ans = ob.pattern(a, N)
		print(ans)


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			if a[i][0:N] == a[i][::-1]:
				return str(i) + ' R'
		for j in range(N):
			wer = ''
			for k in range(N):
				wer += str(a[k][j])
			if wer[:] == wer[::-1]:
				return str(j) + ' C'
			wer = ''
		return -1


class Solution:

	def pattern(self, a, N):
		if N == 1:
			return str(0) + ' R'
		for i in range(0, N):
			for j in range(0, N // 2):
				if a[i][j] == a[i][N - 1 - j]:
					if j == max(N // 2 - 1, 0):
						return str(i) + ' R'
					continue
				else:
					break
		for j in range(0, N):
			for i in range(0, N // 2):
				if a[i][j] == a[N - 1 - i][j]:
					if i == max(N // 2 - 1, 0):
						return str(j) + ' C'
					continue
				else:
					break
		return -1


class Solution:

	def pattern(self, a, N):
		ans = '-1'
		for i in range(N):
			check = 0
			for j in range(int(N / 2) + 1):
				if a[i][j] == a[i][N - j - 1]:
					check += 1
			if check == int(N / 2) + 1:
				ans = str(i) + ' R'
				break
		if ans == '-1':
			for i in range(N):
				check = 0
				for j in range(int(N / 2) + 1):
					if a[j][i] == a[N - j - 1][i]:
						check += 1
				if check == int(N / 2) + 1:
					ans = str(i) + ' C'
					break
		return ans


class Solution:

	def pattern(self, a, n):
		for i in range(0, n):
			cond = True
			for j in range(n // 2):
				if a[i][j] != a[i][n - j - 1]:
					break
			else:
				return str(i) + ' R'
		for i in range(0, n):
			cond = True
			for j in range(n // 2):
				if a[j][i] != a[n - j - 1][i]:
					break
			else:
				return str(i) + ' C'
		return -1


class Solution:

	def pattern(self, a, n):
		for i in range(n):
			(f, s, e) = (0, 0, n - 1)
			while s < e:
				if a[i][s] != a[i][e]:
					f = 1
					break
				s += 1
				e -= 1
			if f == 0:
				return str(i) + ' R'
		for i in range(n):
			(f, s, e) = (0, 0, n - 1)
			while s < e:
				if a[s][i] != a[e][i]:
					f = 1
					break
				s += 1
				e -= 1
			if f == 0:
				return str(i) + ' C'
		return -1


class Solution:

	def pattern(self, a, N):
		for i in range(N):
			iseq = True
			for j in range(N // 2):
				if a[i][j] != a[i][N - 1 - j]:
					iseq = False
					break
			if iseq:
				return str(i) + ' ' + 'R'
		for k in range(N):
			isequal = True
			for l in range(N // 2):
				if a[l][k] != a[N - 1 - l][k]:
					isequal = False
					break
			if isequal:
				return str(k) + ' ' + 'C'
		return -1


class Solution:

	def pattern(self, a, N):
		fl = 0
		res = ''
		for i in range(N):
			don = ''
			for j in range(N):
				don += str(a[i][j])
			if don == don[::-1]:
				res += str(i) + ' ' + 'R'
				fl = 1
				break
		if fl == 0:
			for i in range(N):
				don = ''
				for j in range(N):
					don += str(a[j][i])
				if don == don[::-1]:
					res += str(i) + ' ' + 'C'
					break
		if res == '':
			return '-1'
		return res